mp3_writer.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /*
  2. * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. /*
  19. * Contributed by:
  20. * Toni < buldozer at aufbix dot org >
  21. */
  22. #include "mp3_port.h"
  23. #include <pjmedia/errno.h>
  24. #include <pj/assert.h>
  25. #include <pj/file_access.h>
  26. #include <pj/file_io.h>
  27. #include <pj/log.h>
  28. #include <pj/pool.h>
  29. #include <pj/string.h>
  30. #include <pj/unicode.h>
  31. /* Include BladeDLL declarations */
  32. #include "BladeMP3EncDLL.h"
  33. #define THIS_FILE "mp3_writer.c"
  34. #define SIGNATURE PJMEDIA_SIG_CLASS_PORT_AUD('M','W')
  35. #define BYTES_PER_SAMPLE 2
  36. static struct BladeDLL
  37. {
  38. void *hModule;
  39. int refCount;
  40. BEINITSTREAM beInitStream;
  41. BEENCODECHUNK beEncodeChunk;
  42. BEDEINITSTREAM beDeinitStream;
  43. BECLOSESTREAM beCloseStream;
  44. BEVERSION beVersion;
  45. BEWRITEVBRHEADER beWriteVBRHeader;
  46. BEWRITEINFOTAG beWriteInfoTag;
  47. } BladeDLL;
  48. struct mp3_file_port
  49. {
  50. pjmedia_port base;
  51. pj_size_t total;
  52. pj_oshandle_t fd;
  53. pj_size_t cb_size;
  54. pj_status_t (*cb)(pjmedia_port*, void*);
  55. pj_bool_t subscribed;
  56. pj_bool_t cb_called;
  57. void (*cb2)(pjmedia_port*, void*);
  58. unsigned silence_duration;
  59. pj_str_t mp3_filename;
  60. pjmedia_mp3_encoder_option mp3_option;
  61. unsigned mp3_samples_per_frame;
  62. pj_int16_t *mp3_sample_buf;
  63. unsigned mp3_sample_pos;
  64. HBE_STREAM mp3_stream;
  65. unsigned char *mp3_buf;
  66. };
  67. static pj_status_t file_put_frame(pjmedia_port *this_port,
  68. const pjmedia_frame *frame);
  69. static pj_status_t file_get_frame(pjmedia_port *this_port,
  70. pjmedia_frame *frame);
  71. static pj_status_t file_on_destroy(pjmedia_port *this_port);
  72. #if defined(PJ_WIN32) || defined(_WIN32) || defined(WIN32)
  73. #include <windows.h>
  74. #define DLL_NAME PJ_T("LAME_ENC.DLL")
  75. /*
  76. * Load BladeEncoder DLL.
  77. */
  78. static pj_status_t init_blade_dll(void)
  79. {
  80. if (BladeDLL.refCount == 0) {
  81. #define GET_PROC(type, name) \
  82. BladeDLL.name = (type)GetProcAddress(BladeDLL.hModule, PJ_T(#name)); \
  83. if (BladeDLL.name == NULL) { \
  84. PJ_LOG(1,(THIS_FILE, "Unable to find %s in %s", #name, DLL_NAME)); \
  85. return PJ_RETURN_OS_ERROR(GetLastError()); \
  86. }
  87. BE_VERSION beVersion;
  88. BladeDLL.hModule = (void*)LoadLibrary(DLL_NAME);
  89. if (BladeDLL.hModule == NULL) {
  90. pj_status_t status = PJ_RETURN_OS_ERROR(GetLastError());
  91. char errmsg[PJ_ERR_MSG_SIZE];
  92. pj_strerror(status, errmsg, sizeof(errmsg));
  93. PJ_LOG(1,(THIS_FILE, "Unable to load %s: %s", DLL_NAME, errmsg));
  94. return status;
  95. }
  96. GET_PROC(BEINITSTREAM, beInitStream);
  97. GET_PROC(BEENCODECHUNK, beEncodeChunk);
  98. GET_PROC(BEDEINITSTREAM, beDeinitStream);
  99. GET_PROC(BECLOSESTREAM, beCloseStream);
  100. GET_PROC(BEVERSION, beVersion);
  101. GET_PROC(BEWRITEVBRHEADER, beWriteVBRHeader);
  102. GET_PROC(BEWRITEINFOTAG, beWriteInfoTag);
  103. #undef GET_PROC
  104. BladeDLL.beVersion(&beVersion);
  105. PJ_LOG(4,(THIS_FILE, "%s encoder v%d.%d loaded (%s)", DLL_NAME,
  106. beVersion.byMajorVersion, beVersion.byMinorVersion,
  107. beVersion.zHomepage));
  108. }
  109. ++BladeDLL.refCount;
  110. return PJ_SUCCESS;
  111. }
  112. /*
  113. * Decrement the reference counter of the DLL.
  114. */
  115. static void deinit_blade_dll()
  116. {
  117. --BladeDLL.refCount;
  118. if (BladeDLL.refCount == 0 && BladeDLL.hModule) {
  119. FreeLibrary(BladeDLL.hModule);
  120. BladeDLL.hModule = NULL;
  121. PJ_LOG(4,(THIS_FILE, "%s unloaded", DLL_NAME));
  122. }
  123. }
  124. #else
  125. static pj_status_t init_blade_dll(void)
  126. {
  127. PJ_LOG(1,(THIS_FILE, "Error: MP3 writer port only works on Windows for now"));
  128. return PJ_ENOTSUP;
  129. }
  130. static void deinit_blade_dll()
  131. {
  132. }
  133. #endif
  134. /*
  135. * Initialize MP3 encoder.
  136. */
  137. static pj_status_t init_mp3_encoder(struct mp3_file_port *fport,
  138. pj_pool_t *pool)
  139. {
  140. BE_CONFIG LConfig;
  141. unsigned long InSamples;
  142. unsigned long OutBuffSize;
  143. long MP3Err;
  144. /*
  145. * Initialize encoder configuration.
  146. */
  147. pj_bzero(&LConfig, sizeof(BE_CONFIG));
  148. LConfig.dwConfig = BE_CONFIG_LAME;
  149. LConfig.format.LHV1.dwStructVersion = 1;
  150. LConfig.format.LHV1.dwStructSize = sizeof(BE_CONFIG);
  151. LConfig.format.LHV1.dwSampleRate = PJMEDIA_PIA_SRATE(&fport->base.info);
  152. LConfig.format.LHV1.dwReSampleRate = 0;
  153. if (PJMEDIA_PIA_CCNT(&fport->base.info)==1)
  154. LConfig.format.LHV1.nMode = BE_MP3_MODE_MONO;
  155. else if (PJMEDIA_PIA_CCNT(&fport->base.info)==2)
  156. LConfig.format.LHV1.nMode = BE_MP3_MODE_STEREO;
  157. else
  158. return PJMEDIA_ENCCHANNEL;
  159. LConfig.format.LHV1.dwBitrate = fport->mp3_option.bit_rate / 1000;
  160. LConfig.format.LHV1.nPreset = LQP_NOPRESET;
  161. LConfig.format.LHV1.bCopyright = 0;
  162. LConfig.format.LHV1.bCRC = 1;
  163. LConfig.format.LHV1.bOriginal = 1;
  164. LConfig.format.LHV1.bPrivate = 0;
  165. if (!fport->mp3_option.vbr) {
  166. LConfig.format.LHV1.nVbrMethod = VBR_METHOD_NONE;
  167. LConfig.format.LHV1.bWriteVBRHeader = 0;
  168. LConfig.format.LHV1.bEnableVBR = 0;
  169. } else {
  170. LConfig.format.LHV1.nVbrMethod = VBR_METHOD_DEFAULT;
  171. LConfig.format.LHV1.bWriteVBRHeader = 1;
  172. LConfig.format.LHV1.dwVbrAbr_bps = fport->mp3_option.bit_rate;
  173. LConfig.format.LHV1.nVBRQuality = (pj_uint16_t)
  174. fport->mp3_option.quality;
  175. LConfig.format.LHV1.bEnableVBR = 1;
  176. }
  177. LConfig.format.LHV1.nQuality = (pj_uint16_t)
  178. (((0-fport->mp3_option.quality-1)<<8) |
  179. fport->mp3_option.quality);
  180. /*
  181. * Init MP3 stream.
  182. */
  183. InSamples = 0;
  184. MP3Err = BladeDLL.beInitStream(&LConfig, &InSamples, &OutBuffSize,
  185. &fport->mp3_stream);
  186. if (MP3Err != BE_ERR_SUCCESSFUL)
  187. return PJMEDIA_ERROR;
  188. /*
  189. * Allocate sample buffer.
  190. */
  191. fport->mp3_samples_per_frame = (unsigned)InSamples;
  192. fport->mp3_sample_buf = pj_pool_alloc(pool, fport->mp3_samples_per_frame * 2);
  193. if (!fport->mp3_sample_buf)
  194. return PJ_ENOMEM;
  195. /*
  196. * Allocate encoded MP3 buffer.
  197. */
  198. fport->mp3_buf = pj_pool_alloc(pool, (pj_size_t)OutBuffSize);
  199. if (fport->mp3_buf == NULL)
  200. return PJ_ENOMEM;
  201. return PJ_SUCCESS;
  202. }
  203. /*
  204. * Create MP3 file writer port.
  205. */
  206. PJ_DEF(pj_status_t)
  207. pjmedia_mp3_writer_port_create( pj_pool_t *pool,
  208. const char *filename,
  209. unsigned sampling_rate,
  210. unsigned channel_count,
  211. unsigned samples_per_frame,
  212. unsigned bits_per_sample,
  213. const pjmedia_mp3_encoder_option *param_option,
  214. pjmedia_port **p_port )
  215. {
  216. struct mp3_file_port *fport;
  217. pj_status_t status;
  218. status = init_blade_dll();
  219. if (status != PJ_SUCCESS)
  220. return status;
  221. /* Check arguments. */
  222. PJ_ASSERT_RETURN(pool && filename && p_port, PJ_EINVAL);
  223. /* Only supports 16bits per sample for now. */
  224. PJ_ASSERT_RETURN(bits_per_sample == 16, PJ_EINVAL);
  225. /* Create file port instance. */
  226. fport = pj_pool_zalloc(pool, sizeof(struct mp3_file_port));
  227. PJ_ASSERT_RETURN(fport != NULL, PJ_ENOMEM);
  228. /* Initialize port info. */
  229. pj_strdup2_with_null(pool, &fport->mp3_filename, filename);
  230. pjmedia_port_info_init(&fport->base.info, &fport->mp3_filename, SIGNATURE,
  231. sampling_rate, channel_count, bits_per_sample,
  232. samples_per_frame);
  233. fport->base.get_frame = &file_get_frame;
  234. fport->base.put_frame = &file_put_frame;
  235. fport->base.on_destroy = &file_on_destroy;
  236. /* Open file in write and read mode.
  237. * We need the read mode because we'll modify the WAVE header once
  238. * the recording has completed.
  239. */
  240. status = pj_file_open(pool, filename, PJ_O_WRONLY | PJ_O_CLOEXEC,
  241. &fport->fd);
  242. if (status != PJ_SUCCESS) {
  243. deinit_blade_dll();
  244. return status;
  245. }
  246. /* Copy and initialize option with default settings */
  247. if (param_option) {
  248. pj_memcpy(&fport->mp3_option, param_option,
  249. sizeof(pjmedia_mp3_encoder_option));
  250. } else {
  251. pj_bzero(&fport->mp3_option, sizeof(pjmedia_mp3_encoder_option));
  252. fport->mp3_option.vbr = PJ_TRUE;
  253. }
  254. /* Calculate bitrate if it's not specified, only if it's not VBR. */
  255. if (fport->mp3_option.bit_rate == 0 && !fport->mp3_option.vbr)
  256. fport->mp3_option.bit_rate = sampling_rate * channel_count;
  257. /* Set default quality if it's not specified */
  258. if (fport->mp3_option.quality == 0)
  259. fport->mp3_option.quality = 2;
  260. /* Init mp3 encoder */
  261. status = init_mp3_encoder(fport, pool);
  262. if (status != PJ_SUCCESS) {
  263. pj_file_close(fport->fd);
  264. deinit_blade_dll();
  265. return status;
  266. }
  267. /* Done. */
  268. *p_port = &fport->base;
  269. PJ_LOG(4,(THIS_FILE,
  270. "MP3 file writer '%.*s' created: samp.rate=%dKHz, "
  271. "bitrate=%dkbps%s, quality=%d",
  272. (int)fport->base.info.name.slen,
  273. fport->base.info.name.ptr,
  274. PJMEDIA_PIA_SRATE(&fport->base.info),
  275. fport->mp3_option.bit_rate/1000,
  276. (fport->mp3_option.vbr ? " (VBR)" : ""),
  277. fport->mp3_option.quality));
  278. return PJ_SUCCESS;
  279. }
  280. #if !DEPRECATED_FOR_TICKET_2251
  281. /*
  282. * Register callback.
  283. */
  284. PJ_DEF(pj_status_t)
  285. pjmedia_mp3_writer_port_set_cb( pjmedia_port *port,
  286. pj_size_t pos,
  287. void *user_data,
  288. pj_status_t (*cb)(pjmedia_port *port,
  289. void *usr_data))
  290. {
  291. struct mp3_file_port *fport;
  292. /* Sanity check */
  293. PJ_ASSERT_RETURN(port && cb, PJ_EINVAL);
  294. /* Check that this is really a writer port */
  295. PJ_ASSERT_RETURN(port->info.signature == SIGNATURE, PJ_EINVALIDOP);
  296. PJ_LOG(1, (THIS_FILE, "pjmedia_mp3_writer_port_set_cb() is deprecated. "
  297. "Use pjmedia_mp3_writer_port_set_cb2() instead."));
  298. fport = (struct mp3_file_port*) port;
  299. fport->cb_size = pos;
  300. fport->base.port_data.pdata = user_data;
  301. fport->cb = cb;
  302. return PJ_SUCCESS;
  303. }
  304. #endif
  305. /*
  306. * Register callback.
  307. */
  308. PJ_DEF(pj_status_t)
  309. pjmedia_mp3_writer_port_set_cb2(pjmedia_port *port,
  310. pj_size_t pos,
  311. void *user_data,
  312. void (*cb)(pjmedia_port *port,
  313. void *usr_data))
  314. {
  315. struct mp3_file_port *fport;
  316. /* Sanity check */
  317. PJ_ASSERT_RETURN(port && cb, PJ_EINVAL);
  318. /* Check that this is really a writer port */
  319. PJ_ASSERT_RETURN(port->info.signature == SIGNATURE, PJ_EINVALIDOP);
  320. fport = (struct mp3_file_port*) port;
  321. fport->cb_size = pos;
  322. fport->base.port_data.pdata = user_data;
  323. fport->cb2 = cb;
  324. fport->cb_called = PJ_FALSE;
  325. return PJ_SUCCESS;
  326. }
  327. static pj_status_t file_on_event(pjmedia_event *event,
  328. void *user_data)
  329. {
  330. struct file_port *fport = (struct file_port*)user_data;
  331. if (event->type == PJMEDIA_EVENT_CALLBACK) {
  332. if (fport->cb2)
  333. (*fport->cb2)(&fport->base, fport->base.port_data.pdata);
  334. }
  335. return PJ_SUCCESS;
  336. }
  337. /*
  338. * Put a frame into the buffer. When the buffer is full, flush the buffer
  339. * to the file.
  340. */
  341. static pj_status_t file_put_frame(pjmedia_port *this_port,
  342. const pjmedia_frame *frame)
  343. {
  344. struct mp3_file_port *fport = (struct mp3_file_port *)this_port;
  345. unsigned long MP3Err;
  346. pj_ssize_t bytes;
  347. pj_status_t status;
  348. unsigned long WriteSize;
  349. /* Record silence if input is no-frame */
  350. if (frame->type == PJMEDIA_FRAME_TYPE_NONE || frame->size == 0) {
  351. unsigned samples_left = PJMEDIA_PIA_SPF(&fport->base.info);
  352. unsigned samples_copied = 0;
  353. /* Only want to record at most 1 second of silence */
  354. if (fport->silence_duration >= PJMEDIA_PIA_SRATE(&fport->base.info))
  355. return PJ_SUCCESS;
  356. while (samples_left) {
  357. unsigned samples_needed = fport->mp3_samples_per_frame -
  358. fport->mp3_sample_pos;
  359. if (samples_needed > samples_left)
  360. samples_needed = samples_left;
  361. pjmedia_zero_samples(fport->mp3_sample_buf + fport->mp3_sample_pos,
  362. samples_needed);
  363. fport->mp3_sample_pos += samples_needed;
  364. samples_left -= samples_needed;
  365. samples_copied += samples_needed;
  366. /* Encode if we have full frame */
  367. if (fport->mp3_sample_pos == fport->mp3_samples_per_frame) {
  368. /* Clear position */
  369. fport->mp3_sample_pos = 0;
  370. /* Encode ! */
  371. MP3Err = BladeDLL.beEncodeChunk(fport->mp3_stream,
  372. fport->mp3_samples_per_frame,
  373. fport->mp3_sample_buf,
  374. fport->mp3_buf,
  375. &WriteSize);
  376. if (MP3Err != BE_ERR_SUCCESSFUL)
  377. return PJMEDIA_ERROR;
  378. /* Write the chunk */
  379. bytes = WriteSize;
  380. status = pj_file_write(fport->fd, fport->mp3_buf, &bytes);
  381. if (status != PJ_SUCCESS)
  382. return status;
  383. /* Increment total written. */
  384. fport->total += bytes;
  385. }
  386. }
  387. fport->silence_duration += PJMEDIA_PIA_SPF(&fport->base.info);
  388. }
  389. /* If encoder is expecting different sample size, then we need to
  390. * buffer the samples.
  391. */
  392. else if (fport->mp3_samples_per_frame !=
  393. PJMEDIA_PIA_SPF(&fport->base.info))
  394. {
  395. unsigned samples_left = frame->size / 2;
  396. unsigned samples_copied = 0;
  397. const pj_int16_t *src_samples = frame->buf;
  398. fport->silence_duration = 0;
  399. while (samples_left) {
  400. unsigned samples_needed = fport->mp3_samples_per_frame -
  401. fport->mp3_sample_pos;
  402. if (samples_needed > samples_left)
  403. samples_needed = samples_left;
  404. pjmedia_copy_samples(fport->mp3_sample_buf + fport->mp3_sample_pos,
  405. src_samples + samples_copied,
  406. samples_needed);
  407. fport->mp3_sample_pos += samples_needed;
  408. samples_left -= samples_needed;
  409. samples_copied += samples_needed;
  410. /* Encode if we have full frame */
  411. if (fport->mp3_sample_pos == fport->mp3_samples_per_frame) {
  412. /* Clear position */
  413. fport->mp3_sample_pos = 0;
  414. /* Encode ! */
  415. MP3Err = BladeDLL.beEncodeChunk(fport->mp3_stream,
  416. fport->mp3_samples_per_frame,
  417. fport->mp3_sample_buf,
  418. fport->mp3_buf,
  419. &WriteSize);
  420. if (MP3Err != BE_ERR_SUCCESSFUL)
  421. return PJMEDIA_ERROR;
  422. /* Write the chunk */
  423. bytes = WriteSize;
  424. status = pj_file_write(fport->fd, fport->mp3_buf, &bytes);
  425. if (status != PJ_SUCCESS)
  426. return status;
  427. /* Increment total written. */
  428. fport->total += bytes;
  429. }
  430. }
  431. } else {
  432. fport->silence_duration = 0;
  433. /* Encode ! */
  434. MP3Err = BladeDLL.beEncodeChunk(fport->mp3_stream,
  435. fport->mp3_samples_per_frame,
  436. frame->buf,
  437. fport->mp3_buf,
  438. &WriteSize);
  439. if (MP3Err != BE_ERR_SUCCESSFUL)
  440. return PJMEDIA_ERROR;
  441. /* Write the chunk */
  442. bytes = WriteSize;
  443. status = pj_file_write(fport->fd, fport->mp3_buf, &bytes);
  444. if (status != PJ_SUCCESS)
  445. return status;
  446. /* Increment total written. */
  447. fport->total += bytes;
  448. }
  449. /* Check if we need to call callback */
  450. if (fport->total >= fport->cb_size) {
  451. if (fport->cb2) {
  452. if (!fport->subscribed) {
  453. pj_status_t status;
  454. status = pjmedia_event_subscribe(NULL, &file_on_event,
  455. fport, fport);
  456. fport->subscribed = (status == PJ_SUCCESS)? PJ_TRUE:
  457. PJ_FALSE;
  458. }
  459. if (fport->subscribed && !fport->cb_called) {
  460. pjmedia_event event;
  461. /* To prevent the callback from being called more than once. */
  462. fport->cb_called = PJ_TRUE;
  463. pjmedia_event_init(&event, PJMEDIA_EVENT_CALLBACK,
  464. NULL, fport);
  465. pjmedia_event_publish(NULL, fport, &event,
  466. PJMEDIA_EVENT_PUBLISH_POST_EVENT);
  467. }
  468. } else if (fport->cb) {
  469. pj_status_t (*cb)(pjmedia_port*, void*);
  470. pj_status_t status;
  471. cb = fport->cb;
  472. fport->cb = NULL;
  473. status = (*cb)(this_port, this_port->port_data.pdata);
  474. return status;
  475. }
  476. }
  477. return PJ_SUCCESS;
  478. }
  479. /*
  480. * Get frame, basicy is a no-op operation.
  481. */
  482. static pj_status_t file_get_frame(pjmedia_port *this_port,
  483. pjmedia_frame *frame)
  484. {
  485. PJ_UNUSED_ARG(this_port);
  486. PJ_UNUSED_ARG(frame);
  487. return PJ_EINVALIDOP;
  488. }
  489. /*
  490. * Close the port, modify file header with updated file length.
  491. */
  492. static pj_status_t file_on_destroy(pjmedia_port *this_port)
  493. {
  494. struct mp3_file_port *fport = (struct mp3_file_port*)this_port;
  495. pj_status_t status;
  496. unsigned long WriteSize;
  497. unsigned long MP3Err;
  498. if (fport->subscribed) {
  499. pjmedia_event_unsubscribe(NULL, &file_on_event, fport, fport);
  500. fport->subscribed = PJ_FALSE;
  501. }
  502. /* Close encoder */
  503. MP3Err = BladeDLL.beDeinitStream(fport->mp3_stream, fport->mp3_buf,
  504. &WriteSize);
  505. if (MP3Err == BE_ERR_SUCCESSFUL) {
  506. pj_ssize_t bytes = WriteSize;
  507. status = pj_file_write(fport->fd, fport->mp3_buf, &bytes);
  508. }
  509. /* Close file */
  510. status = pj_file_close(fport->fd);
  511. /* Write additional VBR header */
  512. if (fport->mp3_option.vbr) {
  513. MP3Err = BladeDLL.beWriteVBRHeader(fport->mp3_filename.ptr);
  514. }
  515. /* Decrement DLL reference counter */
  516. deinit_blade_dll();
  517. /* Done. */
  518. return PJ_SUCCESS;
  519. }