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.

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