You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

788 lines
24KB

  1. /*
  2. * DV decoder
  3. * Copyright (c) 2002 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avcodec.h"
  20. #include "dsputil.h"
  21. #include "mpegvideo.h"
  22. #include "simple_idct.h"
  23. #define NTSC_FRAME_SIZE 120000
  24. #define PAL_FRAME_SIZE 144000
  25. #define TEX_VLC_BITS 9
  26. typedef struct DVVideoDecodeContext {
  27. AVCodecContext *avctx;
  28. GetBitContext gb;
  29. VLC *vlc;
  30. int sampling_411; /* 0 = 420, 1 = 411 */
  31. int width, height;
  32. uint8_t *current_picture[3]; /* picture structure */
  33. AVFrame picture;
  34. int linesize[3];
  35. DCTELEM block[5*6][64] __align8;
  36. uint8_t dv_zigzag[2][64];
  37. uint8_t idct_permutation[64];
  38. /* XXX: move it to static storage ? */
  39. uint8_t dv_shift[2][22][64];
  40. void (*idct_put[2])(uint8_t *dest, int line_size, DCTELEM *block);
  41. } DVVideoDecodeContext;
  42. #include "dvdata.h"
  43. static VLC dv_vlc;
  44. /* XXX: also include quantization */
  45. static RL_VLC_ELEM *dv_rl_vlc[1];
  46. static void dv_build_unquantize_tables(DVVideoDecodeContext *s)
  47. {
  48. int i, q, j;
  49. /* NOTE: max left shift is 6 */
  50. for(q = 0; q < 22; q++) {
  51. /* 88 unquant */
  52. for(i = 1; i < 64; i++) {
  53. /* 88 table */
  54. j = s->idct_permutation[i];
  55. s->dv_shift[0][q][j] =
  56. dv_quant_shifts[q][dv_88_areas[i]] + 1;
  57. }
  58. /* 248 unquant */
  59. for(i = 1; i < 64; i++) {
  60. /* 248 table */
  61. s->dv_shift[1][q][i] =
  62. dv_quant_shifts[q][dv_248_areas[i]] + 1;
  63. }
  64. }
  65. }
  66. static int dvvideo_decode_init(AVCodecContext *avctx)
  67. {
  68. DVVideoDecodeContext *s = avctx->priv_data;
  69. MpegEncContext s2;
  70. static int done=0;
  71. if (!done) {
  72. int i;
  73. done = 1;
  74. /* NOTE: as a trick, we use the fact the no codes are unused
  75. to accelerate the parsing of partial codes */
  76. init_vlc(&dv_vlc, TEX_VLC_BITS, NB_DV_VLC,
  77. dv_vlc_len, 1, 1, dv_vlc_bits, 2, 2);
  78. dv_rl_vlc[0] = av_malloc(dv_vlc.table_size * sizeof(RL_VLC_ELEM));
  79. for(i = 0; i < dv_vlc.table_size; i++){
  80. int code= dv_vlc.table[i][0];
  81. int len = dv_vlc.table[i][1];
  82. int level, run;
  83. if(len<0){ //more bits needed
  84. run= 0;
  85. level= code;
  86. } else if (code == (NB_DV_VLC - 1)) {
  87. /* EOB */
  88. run = 0;
  89. level = 256;
  90. } else {
  91. run= dv_vlc_run[code] + 1;
  92. level= dv_vlc_level[code];
  93. }
  94. dv_rl_vlc[0][i].len = len;
  95. dv_rl_vlc[0][i].level = level;
  96. dv_rl_vlc[0][i].run = run;
  97. }
  98. }
  99. /* ugly way to get the idct & scantable */
  100. /* XXX: fix it */
  101. memset(&s2, 0, sizeof(MpegEncContext));
  102. s2.avctx = avctx;
  103. dsputil_init(&s2.dsp, avctx);
  104. if (DCT_common_init(&s2) < 0)
  105. return -1;
  106. s->idct_put[0] = s2.dsp.idct_put;
  107. memcpy(s->idct_permutation, s2.dsp.idct_permutation, 64);
  108. memcpy(s->dv_zigzag[0], s2.intra_scantable.permutated, 64);
  109. /* XXX: use MMX also for idct248 */
  110. s->idct_put[1] = simple_idct248_put;
  111. memcpy(s->dv_zigzag[1], dv_248_zigzag, 64);
  112. /* XXX: do it only for constant case */
  113. dv_build_unquantize_tables(s);
  114. return 0;
  115. }
  116. //#define VLC_DEBUG
  117. typedef struct BlockInfo {
  118. const uint8_t *shift_table;
  119. const uint8_t *scan_table;
  120. uint8_t pos; /* position in block */
  121. uint8_t eob_reached; /* true if EOB has been reached */
  122. uint8_t dct_mode;
  123. uint8_t partial_bit_count;
  124. uint16_t partial_bit_buffer;
  125. int shift_offset;
  126. } BlockInfo;
  127. /* block size in bits */
  128. static const uint16_t block_sizes[6] = {
  129. 112, 112, 112, 112, 80, 80
  130. };
  131. #ifndef ALT_BITSTREAM_READER
  132. #error only works with ALT_BITSTREAM_READER
  133. #endif
  134. /* decode ac coefs */
  135. static void dv_decode_ac(DVVideoDecodeContext *s,
  136. BlockInfo *mb, DCTELEM *block, int last_index)
  137. {
  138. int last_re_index;
  139. int shift_offset = mb->shift_offset;
  140. const uint8_t *scan_table = mb->scan_table;
  141. const uint8_t *shift_table = mb->shift_table;
  142. int pos = mb->pos;
  143. int level, pos1, sign, run;
  144. int partial_bit_count;
  145. OPEN_READER(re, &s->gb);
  146. #ifdef VLC_DEBUG
  147. printf("start\n");
  148. #endif
  149. /* if we must parse a partial vlc, we do it here */
  150. partial_bit_count = mb->partial_bit_count;
  151. if (partial_bit_count > 0) {
  152. uint8_t buf[4];
  153. uint32_t v;
  154. int l, l1;
  155. GetBitContext gb1;
  156. /* build the dummy bit buffer */
  157. l = 16 - partial_bit_count;
  158. UPDATE_CACHE(re, &s->gb);
  159. #ifdef VLC_DEBUG
  160. printf("show=%04x\n", SHOW_UBITS(re, &s->gb, 16));
  161. #endif
  162. v = (mb->partial_bit_buffer << l) | SHOW_UBITS(re, &s->gb, l);
  163. buf[0] = v >> 8;
  164. buf[1] = v;
  165. #ifdef VLC_DEBUG
  166. printf("v=%04x cnt=%d %04x\n",
  167. v, partial_bit_count, (mb->partial_bit_buffer << l));
  168. #endif
  169. /* try to read the codeword */
  170. init_get_bits(&gb1, buf, 4*8);
  171. {
  172. OPEN_READER(re1, &gb1);
  173. UPDATE_CACHE(re1, &gb1);
  174. GET_RL_VLC(level, run, re1, &gb1, dv_rl_vlc[0],
  175. TEX_VLC_BITS, 2);
  176. l = re1_index;
  177. CLOSE_READER(re1, &gb1);
  178. }
  179. #ifdef VLC_DEBUG
  180. printf("****run=%d level=%d size=%d\n", run, level, l);
  181. #endif
  182. /* compute codeword length */
  183. l1 = (level != 256 && level != 0);
  184. /* if too long, we cannot parse */
  185. l -= partial_bit_count;
  186. if ((re_index + l + l1) > last_index)
  187. return;
  188. /* skip read bits */
  189. last_re_index = 0; /* avoid warning */
  190. re_index += l;
  191. /* by definition, if we can read the vlc, all partial bits
  192. will be read (otherwise we could have read the vlc before) */
  193. mb->partial_bit_count = 0;
  194. UPDATE_CACHE(re, &s->gb);
  195. goto handle_vlc;
  196. }
  197. /* get the AC coefficients until last_index is reached */
  198. for(;;) {
  199. UPDATE_CACHE(re, &s->gb);
  200. #ifdef VLC_DEBUG
  201. printf("%2d: bits=%04x index=%d\n",
  202. pos, SHOW_UBITS(re, &s->gb, 16), re_index);
  203. #endif
  204. last_re_index = re_index;
  205. GET_RL_VLC(level, run, re, &s->gb, dv_rl_vlc[0],
  206. TEX_VLC_BITS, 2);
  207. handle_vlc:
  208. #ifdef VLC_DEBUG
  209. printf("run=%d level=%d\n", run, level);
  210. #endif
  211. if (level == 256) {
  212. if (re_index > last_index) {
  213. cannot_read:
  214. /* put position before read code */
  215. re_index = last_re_index;
  216. mb->eob_reached = 0;
  217. break;
  218. }
  219. /* EOB */
  220. mb->eob_reached = 1;
  221. break;
  222. } else if (level != 0) {
  223. if ((re_index + 1) > last_index)
  224. goto cannot_read;
  225. sign = SHOW_SBITS(re, &s->gb, 1);
  226. level = (level ^ sign) - sign;
  227. LAST_SKIP_BITS(re, &s->gb, 1);
  228. pos += run;
  229. /* error */
  230. if (pos >= 64) {
  231. goto read_error;
  232. }
  233. pos1 = scan_table[pos];
  234. level = level << (shift_table[pos1] + shift_offset);
  235. block[pos1] = level;
  236. // printf("run=%d level=%d shift=%d\n", run, level, shift_table[pos1]);
  237. } else {
  238. if (re_index > last_index)
  239. goto cannot_read;
  240. /* level is zero: means run without coding. No
  241. sign is coded */
  242. pos += run;
  243. /* error */
  244. if (pos >= 64) {
  245. read_error:
  246. #if defined(VLC_DEBUG) || 1
  247. printf("error pos=%d\n", pos);
  248. #endif
  249. /* for errors, we consider the eob is reached */
  250. mb->eob_reached = 1;
  251. break;
  252. }
  253. }
  254. }
  255. CLOSE_READER(re, &s->gb);
  256. mb->pos = pos;
  257. }
  258. static inline void bit_copy(PutBitContext *pb, GetBitContext *gb, int bits_left)
  259. {
  260. while (bits_left >= 16) {
  261. put_bits(pb, 16, get_bits(gb, 16));
  262. bits_left -= 16;
  263. }
  264. if (bits_left > 0) {
  265. put_bits(pb, bits_left, get_bits(gb, bits_left));
  266. }
  267. }
  268. /* mb_x and mb_y are in units of 8 pixels */
  269. static inline void dv_decode_video_segment(DVVideoDecodeContext *s,
  270. uint8_t *buf_ptr1,
  271. const uint16_t *mb_pos_ptr)
  272. {
  273. int quant, dc, dct_mode, class1, j;
  274. int mb_index, mb_x, mb_y, v, last_index;
  275. DCTELEM *block, *block1;
  276. int c_offset, bits_left;
  277. uint8_t *y_ptr;
  278. BlockInfo mb_data[5 * 6], *mb, *mb1;
  279. void (*idct_put)(uint8_t *dest, int line_size, DCTELEM *block);
  280. uint8_t *buf_ptr;
  281. PutBitContext pb, vs_pb;
  282. uint8_t mb_bit_buffer[80 + 4]; /* allow some slack */
  283. int mb_bit_count;
  284. uint8_t vs_bit_buffer[5 * 80 + 4]; /* allow some slack */
  285. int vs_bit_count;
  286. memset(s->block, 0, sizeof(s->block));
  287. /* pass 1 : read DC and AC coefficients in blocks */
  288. buf_ptr = buf_ptr1;
  289. block1 = &s->block[0][0];
  290. mb1 = mb_data;
  291. init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80, NULL, NULL);
  292. vs_bit_count = 0;
  293. for(mb_index = 0; mb_index < 5; mb_index++) {
  294. /* skip header */
  295. quant = buf_ptr[3] & 0x0f;
  296. buf_ptr += 4;
  297. init_put_bits(&pb, mb_bit_buffer, 80, NULL, NULL);
  298. mb_bit_count = 0;
  299. mb = mb1;
  300. block = block1;
  301. for(j = 0;j < 6; j++) {
  302. /* NOTE: size is not important here */
  303. init_get_bits(&s->gb, buf_ptr, 14*8);
  304. /* get the dc */
  305. dc = get_bits(&s->gb, 9);
  306. dc = (dc << (32 - 9)) >> (32 - 9);
  307. dct_mode = get_bits1(&s->gb);
  308. mb->dct_mode = dct_mode;
  309. mb->scan_table = s->dv_zigzag[dct_mode];
  310. class1 = get_bits(&s->gb, 2);
  311. mb->shift_offset = (class1 == 3);
  312. mb->shift_table = s->dv_shift[dct_mode]
  313. [quant + dv_quant_offset[class1]];
  314. dc = dc << 2;
  315. /* convert to unsigned because 128 is not added in the
  316. standard IDCT */
  317. dc += 1024;
  318. block[0] = dc;
  319. last_index = block_sizes[j];
  320. buf_ptr += last_index >> 3;
  321. mb->pos = 0;
  322. mb->partial_bit_count = 0;
  323. dv_decode_ac(s, mb, block, last_index);
  324. /* write the remaining bits in a new buffer only if the
  325. block is finished */
  326. bits_left = last_index - s->gb.index;
  327. if (mb->eob_reached) {
  328. mb->partial_bit_count = 0;
  329. mb_bit_count += bits_left;
  330. bit_copy(&pb, &s->gb, bits_left);
  331. } else {
  332. /* should be < 16 bits otherwise a codeword could have
  333. been parsed */
  334. mb->partial_bit_count = bits_left;
  335. mb->partial_bit_buffer = get_bits(&s->gb, bits_left);
  336. }
  337. block += 64;
  338. mb++;
  339. }
  340. flush_put_bits(&pb);
  341. /* pass 2 : we can do it just after */
  342. #ifdef VLC_DEBUG
  343. printf("***pass 2 size=%d\n", mb_bit_count);
  344. #endif
  345. block = block1;
  346. mb = mb1;
  347. init_get_bits(&s->gb, mb_bit_buffer, 80*8);
  348. for(j = 0;j < 6; j++) {
  349. if (!mb->eob_reached && s->gb.index < mb_bit_count) {
  350. dv_decode_ac(s, mb, block, mb_bit_count);
  351. /* if still not finished, no need to parse other blocks */
  352. if (!mb->eob_reached) {
  353. /* we could not parse the current AC coefficient,
  354. so we add the remaining bytes */
  355. bits_left = mb_bit_count - s->gb.index;
  356. if (bits_left > 0) {
  357. mb->partial_bit_count += bits_left;
  358. mb->partial_bit_buffer =
  359. (mb->partial_bit_buffer << bits_left) |
  360. get_bits(&s->gb, bits_left);
  361. }
  362. goto next_mb;
  363. }
  364. }
  365. block += 64;
  366. mb++;
  367. }
  368. /* all blocks are finished, so the extra bytes can be used at
  369. the video segment level */
  370. bits_left = mb_bit_count - s->gb.index;
  371. vs_bit_count += bits_left;
  372. bit_copy(&vs_pb, &s->gb, bits_left);
  373. next_mb:
  374. mb1 += 6;
  375. block1 += 6 * 64;
  376. }
  377. /* we need a pass other the whole video segment */
  378. flush_put_bits(&vs_pb);
  379. #ifdef VLC_DEBUG
  380. printf("***pass 3 size=%d\n", vs_bit_count);
  381. #endif
  382. block = &s->block[0][0];
  383. mb = mb_data;
  384. init_get_bits(&s->gb, vs_bit_buffer, 5 * 80*8);
  385. for(mb_index = 0; mb_index < 5; mb_index++) {
  386. for(j = 0;j < 6; j++) {
  387. if (!mb->eob_reached) {
  388. #ifdef VLC_DEBUG
  389. printf("start %d:%d\n", mb_index, j);
  390. #endif
  391. dv_decode_ac(s, mb, block, vs_bit_count);
  392. }
  393. block += 64;
  394. mb++;
  395. }
  396. }
  397. /* compute idct and place blocks */
  398. block = &s->block[0][0];
  399. mb = mb_data;
  400. for(mb_index = 0; mb_index < 5; mb_index++) {
  401. v = *mb_pos_ptr++;
  402. mb_x = v & 0xff;
  403. mb_y = v >> 8;
  404. y_ptr = s->current_picture[0] + (mb_y * s->linesize[0] * 8) + (mb_x * 8);
  405. if (s->sampling_411)
  406. c_offset = (mb_y * s->linesize[1] * 8) + ((mb_x >> 2) * 8);
  407. else
  408. c_offset = ((mb_y >> 1) * s->linesize[1] * 8) + ((mb_x >> 1) * 8);
  409. for(j = 0;j < 6; j++) {
  410. idct_put = s->idct_put[mb->dct_mode];
  411. if (j < 4) {
  412. if (s->sampling_411 && mb_x < (704 / 8)) {
  413. /* NOTE: at end of line, the macroblock is handled as 420 */
  414. idct_put(y_ptr + (j * 8), s->linesize[0], block);
  415. } else {
  416. idct_put(y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->linesize[0]),
  417. s->linesize[0], block);
  418. }
  419. } else {
  420. if (s->sampling_411 && mb_x >= (704 / 8)) {
  421. uint8_t pixels[64], *c_ptr, *c_ptr1, *ptr;
  422. int y, linesize;
  423. /* NOTE: at end of line, the macroblock is handled as 420 */
  424. idct_put(pixels, 8, block);
  425. linesize = s->linesize[6 - j];
  426. c_ptr = s->current_picture[6 - j] + c_offset;
  427. ptr = pixels;
  428. for(y = 0;y < 8; y++) {
  429. /* convert to 411P */
  430. c_ptr1 = c_ptr + linesize;
  431. c_ptr1[0] = c_ptr[0] = (ptr[0] + ptr[1]) >> 1;
  432. c_ptr1[1] = c_ptr[1] = (ptr[2] + ptr[3]) >> 1;
  433. c_ptr1[2] = c_ptr[2] = (ptr[4] + ptr[5]) >> 1;
  434. c_ptr1[3] = c_ptr[3] = (ptr[6] + ptr[7]) >> 1;
  435. c_ptr += linesize * 2;
  436. ptr += 8;
  437. }
  438. } else {
  439. /* don't ask me why they inverted Cb and Cr ! */
  440. idct_put(s->current_picture[6 - j] + c_offset,
  441. s->linesize[6 - j], block);
  442. }
  443. }
  444. block += 64;
  445. mb++;
  446. }
  447. }
  448. }
  449. /* NOTE: exactly one frame must be given (120000 bytes for NTSC,
  450. 144000 bytes for PAL) */
  451. static int dvvideo_decode_frame(AVCodecContext *avctx,
  452. void *data, int *data_size,
  453. uint8_t *buf, int buf_size)
  454. {
  455. DVVideoDecodeContext *s = avctx->priv_data;
  456. int sct, dsf, apt, ds, nb_dif_segs, vs, width, height, i, packet_size;
  457. uint8_t *buf_ptr;
  458. const uint16_t *mb_pos_ptr;
  459. /* parse id */
  460. init_get_bits(&s->gb, buf, buf_size*8);
  461. sct = get_bits(&s->gb, 3);
  462. if (sct != 0)
  463. return -1;
  464. skip_bits(&s->gb, 5);
  465. get_bits(&s->gb, 4); /* dsn (sequence number */
  466. get_bits(&s->gb, 1); /* fsc (channel number) */
  467. skip_bits(&s->gb, 3);
  468. get_bits(&s->gb, 8); /* dbn (diff block number 0-134) */
  469. dsf = get_bits(&s->gb, 1); /* 0 = NTSC 1 = PAL */
  470. if (get_bits(&s->gb, 1) != 0)
  471. return -1;
  472. skip_bits(&s->gb, 11);
  473. apt = get_bits(&s->gb, 3); /* apt */
  474. get_bits(&s->gb, 1); /* tf1 */
  475. skip_bits(&s->gb, 4);
  476. get_bits(&s->gb, 3); /* ap1 */
  477. get_bits(&s->gb, 1); /* tf2 */
  478. skip_bits(&s->gb, 4);
  479. get_bits(&s->gb, 3); /* ap2 */
  480. get_bits(&s->gb, 1); /* tf3 */
  481. skip_bits(&s->gb, 4);
  482. get_bits(&s->gb, 3); /* ap3 */
  483. /* init size */
  484. width = 720;
  485. if (dsf) {
  486. avctx->frame_rate = 25 * FRAME_RATE_BASE;
  487. packet_size = PAL_FRAME_SIZE;
  488. height = 576;
  489. nb_dif_segs = 12;
  490. } else {
  491. avctx->frame_rate = 30 * FRAME_RATE_BASE;
  492. packet_size = NTSC_FRAME_SIZE;
  493. height = 480;
  494. nb_dif_segs = 10;
  495. }
  496. /* NOTE: we only accept several full frames */
  497. if (buf_size < packet_size)
  498. return -1;
  499. /* NTSC[dsf == 0] is always 720x480, 4:1:1
  500. * PAL[dsf == 1] is always 720x576, 4:2:0 for IEC 68134[apt == 0]
  501. * but for the SMPTE 314M[apt == 1] it is 720x576, 4:1:1
  502. */
  503. s->sampling_411 = !dsf || apt;
  504. if (s->sampling_411) {
  505. mb_pos_ptr = dsf ? dv_place_411P : dv_place_411;
  506. avctx->pix_fmt = PIX_FMT_YUV411P;
  507. } else {
  508. mb_pos_ptr = dv_place_420;
  509. avctx->pix_fmt = PIX_FMT_YUV420P;
  510. }
  511. avctx->width = width;
  512. avctx->height = height;
  513. s->picture.reference= 0;
  514. if(avctx->get_buffer(avctx, &s->picture) < 0) {
  515. fprintf(stderr, "get_buffer() failed\n");
  516. return -1;
  517. }
  518. for(i=0;i<3;i++) {
  519. s->current_picture[i] = s->picture.data[i];
  520. s->linesize[i] = s->picture.linesize[i];
  521. if (!s->current_picture[i])
  522. return -1;
  523. }
  524. s->width = width;
  525. s->height = height;
  526. /* for each DIF segment */
  527. buf_ptr = buf;
  528. for (ds = 0; ds < nb_dif_segs; ds++) {
  529. buf_ptr += 6 * 80; /* skip DIF segment header */
  530. for(vs = 0; vs < 27; vs++) {
  531. if ((vs % 3) == 0) {
  532. /* skip audio block */
  533. buf_ptr += 80;
  534. }
  535. dv_decode_video_segment(s, buf_ptr, mb_pos_ptr);
  536. buf_ptr += 5 * 80;
  537. mb_pos_ptr += 5;
  538. }
  539. }
  540. emms_c();
  541. /* return image */
  542. *data_size = sizeof(AVFrame);
  543. *(AVFrame*)data= s->picture;
  544. avctx->release_buffer(avctx, &s->picture);
  545. return packet_size;
  546. }
  547. static int dvvideo_decode_end(AVCodecContext *avctx)
  548. {
  549. DVVideoDecodeContext *s = avctx->priv_data;
  550. int i;
  551. if(avctx->get_buffer == avcodec_default_get_buffer){
  552. for(i=0; i<4; i++){
  553. av_freep(&s->picture.base[i]);
  554. s->picture.data[i]= NULL;
  555. }
  556. av_freep(&s->picture.opaque);
  557. }
  558. return 0;
  559. }
  560. AVCodec dvvideo_decoder = {
  561. "dvvideo",
  562. CODEC_TYPE_VIDEO,
  563. CODEC_ID_DVVIDEO,
  564. sizeof(DVVideoDecodeContext),
  565. dvvideo_decode_init,
  566. NULL,
  567. dvvideo_decode_end,
  568. dvvideo_decode_frame,
  569. CODEC_CAP_DR1,
  570. NULL
  571. };
  572. typedef struct DVAudioDecodeContext {
  573. AVCodecContext *avctx;
  574. GetBitContext gb;
  575. } DVAudioDecodeContext;
  576. static int dvaudio_decode_init(AVCodecContext *avctx)
  577. {
  578. // DVAudioDecodeContext *s = avctx->priv_data;
  579. return 0;
  580. }
  581. static uint16_t dv_audio_12to16(uint16_t sample)
  582. {
  583. uint16_t shift, result;
  584. sample = (sample < 0x800) ? sample : sample | 0xf000;
  585. shift = (sample & 0xf00) >> 8;
  586. if (shift < 0x2 || shift > 0xd) {
  587. result = sample;
  588. } else if (shift < 0x8) {
  589. shift--;
  590. result = (sample - (256 * shift)) << shift;
  591. } else {
  592. shift = 0xe - shift;
  593. result = ((sample + ((256 * shift) + 1)) << shift) - 1;
  594. }
  595. return result;
  596. }
  597. /* NOTE: exactly one frame must be given (120000 bytes for NTSC,
  598. 144000 bytes for PAL)
  599. There's a couple of assumptions being made here:
  600. 1. We don't do any kind of audio error correction. It means,
  601. that erroneous samples 0x8000 are being passed upwards.
  602. Do we need to silence erroneous samples ? Average them ?
  603. 2. We don't do software emphasis.
  604. 3. We are not checking for 'speed' argument being valid.
  605. 4. Audio is always returned as 16bit linear samples: 12bit
  606. nonlinear samples are converted into 16bit linear ones.
  607. */
  608. static int dvaudio_decode_frame(AVCodecContext *avctx,
  609. void *data, int *data_size,
  610. uint8_t *buf, int buf_size)
  611. {
  612. DVVideoDecodeContext *s = avctx->priv_data;
  613. const uint16_t (*unshuffle)[9];
  614. int smpls, freq, quant, sys, stride, difseg, ad, dp, nb_dif_segs, i;
  615. uint16_t lc, rc;
  616. uint8_t *buf_ptr;
  617. /* parse id */
  618. init_get_bits(&s->gb, &buf[AAUX_OFFSET], 5*8);
  619. i = get_bits(&s->gb, 8);
  620. if (i != 0x50) { /* No audio ? */
  621. *data_size = 0;
  622. return buf_size;
  623. }
  624. get_bits(&s->gb, 1); /* 0 - locked audio, 1 - unlocked audio */
  625. skip_bits(&s->gb, 1);
  626. smpls = get_bits(&s->gb, 6); /* samples in this frame - min. samples */
  627. skip_bits(&s->gb, 8);
  628. skip_bits(&s->gb, 2);
  629. sys = get_bits(&s->gb, 1); /* 0 - 60 fields, 1 = 50 fields */
  630. skip_bits(&s->gb, 5);
  631. get_bits(&s->gb, 1); /* 0 - emphasis on, 1 - emphasis off */
  632. get_bits(&s->gb, 1); /* 0 - reserved, 1 - emphasis time constant 50/15us */
  633. freq = get_bits(&s->gb, 3); /* 0 - 48KHz, 1 - 44,1kHz, 2 - 32 kHz */
  634. quant = get_bits(&s->gb, 3); /* 0 - 16bit linear, 1 - 12bit nonlinear */
  635. if (quant > 1)
  636. return -1; /* Unsupported quantization */
  637. avctx->sample_rate = dv_audio_frequency[freq];
  638. // What about:
  639. // avctx->bit_rate =
  640. // avctx->frame_size =
  641. *data_size = (dv_audio_min_samples[sys][freq] + smpls) *
  642. avctx->channels * 2;
  643. if (sys) {
  644. nb_dif_segs = 12;
  645. stride = 108;
  646. unshuffle = dv_place_audio50;
  647. } else {
  648. nb_dif_segs = 10;
  649. stride = 90;
  650. unshuffle = dv_place_audio60;
  651. }
  652. /* for each DIF segment */
  653. buf_ptr = buf;
  654. for (difseg = 0; difseg < nb_dif_segs; difseg++) {
  655. buf_ptr += 6 * 80; /* skip DIF segment header */
  656. for (ad = 0; ad < 9; ad++) {
  657. for (dp = 8; dp < 80; dp+=2) {
  658. if (quant == 0) { /* 16bit quantization */
  659. i = unshuffle[difseg][ad] + (dp - 8)/2 * stride;
  660. ((short *)data)[i] = (buf_ptr[dp] << 8) | buf_ptr[dp+1];
  661. } else { /* 12bit quantization */
  662. if (difseg >= nb_dif_segs/2)
  663. goto out; /* We're not doing 4ch at this time */
  664. lc = ((uint16_t)buf_ptr[dp] << 4) |
  665. ((uint16_t)buf_ptr[dp+2] >> 4);
  666. rc = ((uint16_t)buf_ptr[dp+1] << 4) |
  667. ((uint16_t)buf_ptr[dp+2] & 0x0f);
  668. lc = dv_audio_12to16(lc);
  669. rc = dv_audio_12to16(rc);
  670. i = unshuffle[difseg][ad] + (dp - 8)/3 * stride;
  671. ((short *)data)[i] = lc;
  672. i = unshuffle[difseg+nb_dif_segs/2][ad] + (dp - 8)/3 * stride;
  673. ((short *)data)[i] = rc;
  674. ++dp;
  675. }
  676. }
  677. buf_ptr += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
  678. }
  679. }
  680. out:
  681. return buf_size;
  682. }
  683. static int dvaudio_decode_end(AVCodecContext *avctx)
  684. {
  685. // DVAudioDecodeContext *s = avctx->priv_data;
  686. return 0;
  687. }
  688. AVCodec dvaudio_decoder = {
  689. "dvaudio",
  690. CODEC_TYPE_AUDIO,
  691. CODEC_ID_DVAUDIO,
  692. sizeof(DVAudioDecodeContext),
  693. dvaudio_decode_init,
  694. NULL,
  695. dvaudio_decode_end,
  696. dvaudio_decode_frame,
  697. 0,
  698. NULL
  699. };