dvdec.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. /*
  2. * DV decoder
  3. * Copyright (c) 2002 Fabrice Bellard
  4. * Copyright (c) 2004 Roman Shaposhnik
  5. *
  6. * 50 Mbps (DVCPRO50) support
  7. * Copyright (c) 2006 Daniel Maas <dmaas@maasdigital.com>
  8. *
  9. * 100 Mbps (DVCPRO HD) support
  10. * Initial code by Daniel Maas <dmaas@maasdigital.com> (funded by BBC R&D)
  11. * Final code by Roman Shaposhnik
  12. *
  13. * Many thanks to Dan Dennedy <dan@dennedy.org> for providing wealth
  14. * of DV technical info.
  15. *
  16. * This file is part of FFmpeg.
  17. *
  18. * FFmpeg is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU Lesser General Public
  20. * License as published by the Free Software Foundation; either
  21. * version 2.1 of the License, or (at your option) any later version.
  22. *
  23. * FFmpeg is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  26. * Lesser General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Lesser General Public
  29. * License along with FFmpeg; if not, write to the Free Software
  30. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  31. */
  32. /**
  33. * @file
  34. * DV decoder
  35. */
  36. #include "libavutil/avassert.h"
  37. #include "libavutil/internal.h"
  38. #include "libavutil/mem_internal.h"
  39. #include "libavutil/thread.h"
  40. #include "avcodec.h"
  41. #include "codec_internal.h"
  42. #include "decode.h"
  43. #include "dv.h"
  44. #include "dv_internal.h"
  45. #include "dv_profile_internal.h"
  46. #include "dvdata.h"
  47. #include "get_bits.h"
  48. #include "idctdsp.h"
  49. #include "put_bits.h"
  50. #include "simple_idct.h"
  51. #include "thread.h"
  52. typedef struct BlockInfo {
  53. const uint32_t *factor_table;
  54. const uint8_t *scan_table;
  55. uint8_t pos; /* position in block */
  56. void (*idct_put)(uint8_t *dest, ptrdiff_t stride, int16_t *block);
  57. uint8_t partial_bit_count;
  58. uint32_t partial_bit_buffer;
  59. int shift_offset;
  60. } BlockInfo;
  61. typedef struct DVDecContext {
  62. const AVDVProfile *sys;
  63. const AVFrame *frame;
  64. const uint8_t *buf;
  65. uint8_t dv_zigzag[2][64];
  66. DVwork_chunk work_chunks[4 * 12 * 27];
  67. uint32_t idct_factor[2 * 4 * 16 * 64];
  68. void (*idct_put[2])(uint8_t *dest, ptrdiff_t stride, int16_t *block);
  69. IDCTDSPContext idsp;
  70. } DVDecContext;
  71. static const int dv_iweight_bits = 14;
  72. static const uint16_t dv_iweight_88[64] = {
  73. 32768, 16705, 16705, 17734, 17032, 17734, 18205, 18081,
  74. 18081, 18205, 18725, 18562, 19195, 18562, 18725, 19266,
  75. 19091, 19705, 19705, 19091, 19266, 21407, 19643, 20267,
  76. 20228, 20267, 19643, 21407, 22725, 21826, 20853, 20806,
  77. 20806, 20853, 21826, 22725, 23170, 23170, 21407, 21400,
  78. 21407, 23170, 23170, 24598, 23786, 22018, 22018, 23786,
  79. 24598, 25251, 24465, 22654, 24465, 25251, 25972, 25172,
  80. 25172, 25972, 26722, 27969, 26722, 29692, 29692, 31521,
  81. };
  82. static const uint16_t dv_iweight_248[64] = {
  83. 32768, 16384, 16705, 16705, 17734, 17734, 17734, 17734,
  84. 18081, 18081, 18725, 18725, 21407, 21407, 19091, 19091,
  85. 19195, 19195, 18205, 18205, 18725, 18725, 19705, 19705,
  86. 20267, 20267, 21826, 21826, 23170, 23170, 20806, 20806,
  87. 20267, 20267, 19266, 19266, 21407, 21407, 20853, 20853,
  88. 21400, 21400, 23786, 23786, 24465, 24465, 22018, 22018,
  89. 23170, 23170, 22725, 22725, 24598, 24598, 24465, 24465,
  90. 25172, 25172, 27969, 27969, 25972, 25972, 29692, 29692
  91. };
  92. /**
  93. * The "inverse" DV100 weights are actually just the spec weights (zig-zagged).
  94. */
  95. static const uint16_t dv_iweight_1080_y[64] = {
  96. 128, 16, 16, 17, 17, 17, 18, 18,
  97. 18, 18, 18, 18, 19, 18, 18, 19,
  98. 19, 19, 19, 19, 19, 42, 38, 40,
  99. 40, 40, 38, 42, 44, 43, 41, 41,
  100. 41, 41, 43, 44, 45, 45, 42, 42,
  101. 42, 45, 45, 48, 46, 43, 43, 46,
  102. 48, 49, 48, 44, 48, 49, 101, 98,
  103. 98, 101, 104, 109, 104, 116, 116, 123,
  104. };
  105. static const uint16_t dv_iweight_1080_c[64] = {
  106. 128, 16, 16, 17, 17, 17, 25, 25,
  107. 25, 25, 26, 25, 26, 25, 26, 26,
  108. 26, 27, 27, 26, 26, 42, 38, 40,
  109. 40, 40, 38, 42, 44, 43, 41, 41,
  110. 41, 41, 43, 44, 91, 91, 84, 84,
  111. 84, 91, 91, 96, 93, 86, 86, 93,
  112. 96, 197, 191, 177, 191, 197, 203, 197,
  113. 197, 203, 209, 219, 209, 232, 232, 246,
  114. };
  115. static const uint16_t dv_iweight_720_y[64] = {
  116. 128, 16, 16, 17, 17, 17, 18, 18,
  117. 18, 18, 18, 18, 19, 18, 18, 19,
  118. 19, 19, 19, 19, 19, 42, 38, 40,
  119. 40, 40, 38, 42, 44, 43, 41, 41,
  120. 41, 41, 43, 44, 68, 68, 63, 63,
  121. 63, 68, 68, 96, 92, 86, 86, 92,
  122. 96, 98, 96, 88, 96, 98, 202, 196,
  123. 196, 202, 208, 218, 208, 232, 232, 246,
  124. };
  125. static const uint16_t dv_iweight_720_c[64] = {
  126. 128, 24, 24, 26, 26, 26, 36, 36,
  127. 36, 36, 36, 36, 38, 36, 36, 38,
  128. 38, 38, 38, 38, 38, 84, 76, 80,
  129. 80, 80, 76, 84, 88, 86, 82, 82,
  130. 82, 82, 86, 88, 182, 182, 168, 168,
  131. 168, 182, 182, 192, 186, 192, 172, 186,
  132. 192, 394, 382, 354, 382, 394, 406, 394,
  133. 394, 406, 418, 438, 418, 464, 464, 492,
  134. };
  135. #define TEX_VLC_BITS 10
  136. /* XXX: also include quantization */
  137. static RL_VLC_ELEM dv_rl_vlc[1664];
  138. static av_cold void dv_init_static(void)
  139. {
  140. VLCElem vlc_buf[FF_ARRAY_ELEMS(dv_rl_vlc)] = { 0 };
  141. VLC dv_vlc = { .table = vlc_buf, .table_allocated = FF_ARRAY_ELEMS(vlc_buf) };
  142. const unsigned offset = FF_ARRAY_ELEMS(dv_rl_vlc) - (2 * NB_DV_VLC - NB_DV_ZERO_LEVEL_ENTRIES);
  143. RL_VLC_ELEM *tmp = dv_rl_vlc + offset;
  144. int i, j;
  145. /* it's faster to include sign bit in a generic VLC parsing scheme */
  146. for (i = 0, j = 0; i < NB_DV_VLC; i++, j++) {
  147. tmp[j].len = ff_dv_vlc_len[i];
  148. tmp[j].run = ff_dv_vlc_run[i];
  149. tmp[j].level = ff_dv_vlc_level[i];
  150. if (ff_dv_vlc_level[i]) {
  151. tmp[j].len++;
  152. j++;
  153. tmp[j].len = ff_dv_vlc_len[i] + 1;
  154. tmp[j].run = ff_dv_vlc_run[i];
  155. tmp[j].level = -ff_dv_vlc_level[i];
  156. }
  157. }
  158. /* NOTE: as a trick, we use the fact the no codes are unused
  159. * to accelerate the parsing of partial codes */
  160. ff_init_vlc_from_lengths(&dv_vlc, TEX_VLC_BITS, j,
  161. &tmp[0].len, sizeof(tmp[0]),
  162. NULL, 0, 0, 0, INIT_VLC_USE_NEW_STATIC, NULL);
  163. av_assert1(dv_vlc.table_size == 1664);
  164. for (int i = 0; i < dv_vlc.table_size; i++) {
  165. int code = dv_vlc.table[i].sym;
  166. int len = dv_vlc.table[i].len;
  167. int level, run;
  168. if (len < 0) { // more bits needed
  169. run = 0;
  170. level = code;
  171. } else {
  172. av_assert1(i <= code + offset);
  173. run = tmp[code].run + 1;
  174. level = tmp[code].level;
  175. }
  176. dv_rl_vlc[i].len = len;
  177. dv_rl_vlc[i].level = level;
  178. dv_rl_vlc[i].run = run;
  179. }
  180. }
  181. static void dv_init_weight_tables(DVDecContext *ctx, const AVDVProfile *d)
  182. {
  183. int j, i, c, s;
  184. uint32_t *factor1 = &ctx->idct_factor[0],
  185. *factor2 = &ctx->idct_factor[DV_PROFILE_IS_HD(d) ? 4096 : 2816];
  186. if (DV_PROFILE_IS_HD(d)) {
  187. /* quantization quanta by QNO for DV100 */
  188. static const uint8_t dv100_qstep[16] = {
  189. 1, /* QNO = 0 and 1 both have no quantization */
  190. 1,
  191. 2, 3, 4, 5, 6, 7, 8, 16, 18, 20, 22, 24, 28, 52
  192. };
  193. const uint16_t *iweight1, *iweight2;
  194. if (d->height == 720) {
  195. iweight1 = &dv_iweight_720_y[0];
  196. iweight2 = &dv_iweight_720_c[0];
  197. } else {
  198. iweight1 = &dv_iweight_1080_y[0];
  199. iweight2 = &dv_iweight_1080_c[0];
  200. }
  201. for (c = 0; c < 4; c++) {
  202. for (s = 0; s < 16; s++) {
  203. for (i = 0; i < 64; i++) {
  204. *factor1++ = (dv100_qstep[s] << (c + 9)) * iweight1[i];
  205. *factor2++ = (dv100_qstep[s] << (c + 9)) * iweight2[i];
  206. }
  207. }
  208. }
  209. } else {
  210. static const uint8_t dv_quant_areas[4] = { 6, 21, 43, 64 };
  211. const uint16_t *iweight1 = &dv_iweight_88[0];
  212. for (j = 0; j < 2; j++, iweight1 = &dv_iweight_248[0]) {
  213. for (s = 0; s < 22; s++) {
  214. for (i = c = 0; c < 4; c++) {
  215. for (; i < dv_quant_areas[c]; i++) {
  216. *factor1 = iweight1[i] << (ff_dv_quant_shifts[s][c] + 1);
  217. *factor2++ = (*factor1++) << 1;
  218. }
  219. }
  220. }
  221. }
  222. }
  223. }
  224. static av_cold int dvvideo_decode_init(AVCodecContext *avctx)
  225. {
  226. static AVOnce init_static_once = AV_ONCE_INIT;
  227. DVDecContext *s = avctx->priv_data;
  228. int i;
  229. avctx->chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
  230. ff_idctdsp_init(&s->idsp, avctx);
  231. for (i = 0; i < 64; i++)
  232. s->dv_zigzag[0][i] = s->idsp.idct_permutation[ff_zigzag_direct[i]];
  233. if (avctx->lowres){
  234. for (i = 0; i < 64; i++){
  235. int j = ff_dv_zigzag248_direct[i];
  236. s->dv_zigzag[1][i] = s->idsp.idct_permutation[(j & 7) + (j & 8) * 4 + (j & 48) / 2];
  237. }
  238. }else
  239. memcpy(s->dv_zigzag[1], ff_dv_zigzag248_direct, sizeof(s->dv_zigzag[1]));
  240. s->idct_put[0] = s->idsp.idct_put;
  241. s->idct_put[1] = ff_simple_idct248_put;
  242. ff_thread_once(&init_static_once, dv_init_static);
  243. return 0;
  244. }
  245. /* decode AC coefficients */
  246. static void dv_decode_ac(GetBitContext *gb, BlockInfo *mb, int16_t *block)
  247. {
  248. int last_index = gb->size_in_bits;
  249. const uint8_t *scan_table = mb->scan_table;
  250. const uint32_t *factor_table = mb->factor_table;
  251. int pos = mb->pos;
  252. int partial_bit_count = mb->partial_bit_count;
  253. int level, run, vlc_len, index;
  254. OPEN_READER_NOSIZE(re, gb);
  255. UPDATE_CACHE(re, gb);
  256. /* if we must parse a partial VLC, we do it here */
  257. if (partial_bit_count > 0) {
  258. re_cache = re_cache >> partial_bit_count |
  259. mb->partial_bit_buffer;
  260. re_index -= partial_bit_count;
  261. mb->partial_bit_count = 0;
  262. }
  263. /* get the AC coefficients until last_index is reached */
  264. for (;;) {
  265. ff_dlog(NULL, "%2d: bits=%04"PRIx32" index=%u\n",
  266. pos, SHOW_UBITS(re, gb, 16), re_index);
  267. /* our own optimized GET_RL_VLC */
  268. index = NEG_USR32(re_cache, TEX_VLC_BITS);
  269. vlc_len = dv_rl_vlc[index].len;
  270. if (vlc_len < 0) {
  271. index = NEG_USR32((unsigned) re_cache << TEX_VLC_BITS, -vlc_len) +
  272. dv_rl_vlc[index].level;
  273. vlc_len = TEX_VLC_BITS - vlc_len;
  274. }
  275. level = dv_rl_vlc[index].level;
  276. run = dv_rl_vlc[index].run;
  277. /* gotta check if we're still within gb boundaries */
  278. if (re_index + vlc_len > last_index) {
  279. /* should be < 16 bits otherwise a codeword could have been parsed */
  280. mb->partial_bit_count = last_index - re_index;
  281. mb->partial_bit_buffer = re_cache & ~(-1u >> mb->partial_bit_count);
  282. re_index = last_index;
  283. break;
  284. }
  285. re_index += vlc_len;
  286. ff_dlog(NULL, "run=%d level=%d\n", run, level);
  287. pos += run;
  288. if (pos >= 64)
  289. break;
  290. level = (level * factor_table[pos] + (1 << (dv_iweight_bits - 1))) >>
  291. dv_iweight_bits;
  292. block[scan_table[pos]] = level;
  293. UPDATE_CACHE(re, gb);
  294. }
  295. CLOSE_READER(re, gb);
  296. mb->pos = pos;
  297. }
  298. static inline void bit_copy(PutBitContext *pb, GetBitContext *gb)
  299. {
  300. int bits_left = get_bits_left(gb);
  301. while (bits_left >= MIN_CACHE_BITS) {
  302. put_bits(pb, MIN_CACHE_BITS, get_bits(gb, MIN_CACHE_BITS));
  303. bits_left -= MIN_CACHE_BITS;
  304. }
  305. if (bits_left > 0)
  306. put_bits(pb, bits_left, get_bits(gb, bits_left));
  307. }
  308. static av_always_inline void put_block_8x4(int16_t *block, uint8_t *av_restrict p, int stride)
  309. {
  310. int i, j;
  311. for (i = 0; i < 4; i++) {
  312. for (j = 0; j < 8; j++)
  313. p[j] = av_clip_uint8(block[j]);
  314. block += 8;
  315. p += stride;
  316. }
  317. }
  318. static void dv100_idct_put_last_row_field_chroma(const DVDecContext *s, uint8_t *data,
  319. int stride, int16_t *blocks)
  320. {
  321. s->idsp.idct(blocks + 0*64);
  322. s->idsp.idct(blocks + 1*64);
  323. put_block_8x4(blocks+0*64, data, stride<<1);
  324. put_block_8x4(blocks+0*64 + 4*8, data + 8, stride<<1);
  325. put_block_8x4(blocks+1*64, data + stride, stride<<1);
  326. put_block_8x4(blocks+1*64 + 4*8, data + 8 + stride, stride<<1);
  327. }
  328. static void dv100_idct_put_last_row_field_luma(const DVDecContext *s, uint8_t *data,
  329. int stride, int16_t *blocks)
  330. {
  331. s->idsp.idct(blocks + 0*64);
  332. s->idsp.idct(blocks + 1*64);
  333. s->idsp.idct(blocks + 2*64);
  334. s->idsp.idct(blocks + 3*64);
  335. put_block_8x4(blocks+0*64, data, stride<<1);
  336. put_block_8x4(blocks+0*64 + 4*8, data + 16, stride<<1);
  337. put_block_8x4(blocks+1*64, data + 8, stride<<1);
  338. put_block_8x4(blocks+1*64 + 4*8, data + 24, stride<<1);
  339. put_block_8x4(blocks+2*64, data + stride, stride<<1);
  340. put_block_8x4(blocks+2*64 + 4*8, data + 16 + stride, stride<<1);
  341. put_block_8x4(blocks+3*64, data + 8 + stride, stride<<1);
  342. put_block_8x4(blocks+3*64 + 4*8, data + 24 + stride, stride<<1);
  343. }
  344. /* mb_x and mb_y are in units of 8 pixels */
  345. static int dv_decode_video_segment(AVCodecContext *avctx, void *arg)
  346. {
  347. const DVDecContext *s = avctx->priv_data;
  348. DVwork_chunk *work_chunk = arg;
  349. int quant, dc, dct_mode, class1, j;
  350. int mb_index, mb_x, mb_y, last_index;
  351. int y_stride, linesize;
  352. int16_t *block, *block1;
  353. int c_offset;
  354. uint8_t *y_ptr;
  355. const uint8_t *buf_ptr;
  356. PutBitContext pb, vs_pb;
  357. GetBitContext gb;
  358. BlockInfo mb_data[5 * DV_MAX_BPM], *mb, *mb1;
  359. LOCAL_ALIGNED_16(int16_t, sblock, [5 * DV_MAX_BPM], [64]);
  360. LOCAL_ALIGNED_16(uint8_t, mb_bit_buffer, [80 + AV_INPUT_BUFFER_PADDING_SIZE]); /* allow some slack */
  361. LOCAL_ALIGNED_16(uint8_t, vs_bit_buffer, [80 * 5 + AV_INPUT_BUFFER_PADDING_SIZE]); /* allow some slack */
  362. const int log2_blocksize = 3 - avctx->lowres;
  363. int is_field_mode[5];
  364. int vs_bit_buffer_damaged = 0;
  365. int mb_bit_buffer_damaged[5] = {0};
  366. int retried = 0;
  367. int sta;
  368. av_assert1((((uintptr_t) mb_bit_buffer) & 7) == 0);
  369. av_assert1((((uintptr_t) vs_bit_buffer) & 7) == 0);
  370. retry:
  371. memset(sblock, 0, 5 * DV_MAX_BPM * sizeof(*sblock));
  372. /* pass 1: read DC and AC coefficients in blocks */
  373. buf_ptr = &s->buf[work_chunk->buf_offset * 80];
  374. block1 = &sblock[0][0];
  375. mb1 = mb_data;
  376. init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80);
  377. for (mb_index = 0; mb_index < 5; mb_index++, mb1 += s->sys->bpm, block1 += s->sys->bpm * 64) {
  378. /* skip header */
  379. quant = buf_ptr[3] & 0x0f;
  380. if (avctx->error_concealment) {
  381. if ((buf_ptr[3] >> 4) == 0x0E)
  382. vs_bit_buffer_damaged = 1;
  383. if (!mb_index) {
  384. sta = buf_ptr[3] >> 4;
  385. } else if (sta != (buf_ptr[3] >> 4))
  386. vs_bit_buffer_damaged = 1;
  387. }
  388. buf_ptr += 4;
  389. init_put_bits(&pb, mb_bit_buffer, 80);
  390. mb = mb1;
  391. block = block1;
  392. is_field_mode[mb_index] = 0;
  393. for (j = 0; j < s->sys->bpm; j++) {
  394. last_index = s->sys->block_sizes[j];
  395. init_get_bits(&gb, buf_ptr, last_index);
  396. /* get the DC */
  397. dc = get_sbits(&gb, 9);
  398. dct_mode = get_bits1(&gb);
  399. class1 = get_bits(&gb, 2);
  400. if (DV_PROFILE_IS_HD(s->sys)) {
  401. mb->idct_put = s->idct_put[0];
  402. mb->scan_table = s->dv_zigzag[0];
  403. mb->factor_table = &s->idct_factor[(j >= 4) * 4 * 16 * 64 +
  404. class1 * 16 * 64 +
  405. quant * 64];
  406. is_field_mode[mb_index] |= !j && dct_mode;
  407. } else {
  408. mb->idct_put = s->idct_put[dct_mode && log2_blocksize == 3];
  409. mb->scan_table = s->dv_zigzag[dct_mode];
  410. mb->factor_table =
  411. &s->idct_factor[(class1 == 3) * 2 * 22 * 64 +
  412. dct_mode * 22 * 64 +
  413. (quant + ff_dv_quant_offset[class1]) * 64];
  414. }
  415. dc = dc * 4;
  416. /* convert to unsigned because 128 is not added in the
  417. * standard IDCT */
  418. dc += 1024;
  419. block[0] = dc;
  420. buf_ptr += last_index >> 3;
  421. mb->pos = 0;
  422. mb->partial_bit_count = 0;
  423. ff_dlog(avctx, "MB block: %d, %d ", mb_index, j);
  424. dv_decode_ac(&gb, mb, block);
  425. /* write the remaining bits in a new buffer only if the
  426. * block is finished */
  427. if (mb->pos >= 64)
  428. bit_copy(&pb, &gb);
  429. if (mb->pos >= 64 && mb->pos < 127)
  430. vs_bit_buffer_damaged = mb_bit_buffer_damaged[mb_index] = 1;
  431. block += 64;
  432. mb++;
  433. }
  434. if (mb_bit_buffer_damaged[mb_index] > 0)
  435. continue;
  436. /* pass 2: we can do it just after */
  437. ff_dlog(avctx, "***pass 2 size=%d MB#=%d\n", put_bits_count(&pb), mb_index);
  438. block = block1;
  439. mb = mb1;
  440. init_get_bits(&gb, mb_bit_buffer, put_bits_count(&pb));
  441. put_bits32(&pb, 0); // padding must be zeroed
  442. flush_put_bits(&pb);
  443. for (j = 0; j < s->sys->bpm; j++, block += 64, mb++) {
  444. if (mb->pos < 64 && get_bits_left(&gb) > 0) {
  445. dv_decode_ac(&gb, mb, block);
  446. /* if still not finished, no need to parse other blocks */
  447. if (mb->pos < 64)
  448. break;
  449. if (mb->pos < 127)
  450. vs_bit_buffer_damaged = mb_bit_buffer_damaged[mb_index] = 1;
  451. }
  452. }
  453. /* all blocks are finished, so the extra bytes can be used at
  454. * the video segment level */
  455. if (j >= s->sys->bpm)
  456. bit_copy(&vs_pb, &gb);
  457. }
  458. /* we need a pass over the whole video segment */
  459. ff_dlog(avctx, "***pass 3 size=%d\n", put_bits_count(&vs_pb));
  460. block = &sblock[0][0];
  461. mb = mb_data;
  462. init_get_bits(&gb, vs_bit_buffer, put_bits_count(&vs_pb));
  463. put_bits32(&vs_pb, 0); // padding must be zeroed
  464. flush_put_bits(&vs_pb);
  465. for (mb_index = 0; mb_index < 5; mb_index++) {
  466. for (j = 0; j < s->sys->bpm; j++) {
  467. if (mb->pos < 64 && get_bits_left(&gb) > 0 && !vs_bit_buffer_damaged) {
  468. ff_dlog(avctx, "start %d:%d\n", mb_index, j);
  469. dv_decode_ac(&gb, mb, block);
  470. }
  471. if (mb->pos >= 64 && mb->pos < 127) {
  472. av_log(avctx, AV_LOG_ERROR,
  473. "AC EOB marker is absent pos=%d\n", mb->pos);
  474. vs_bit_buffer_damaged = 1;
  475. }
  476. block += 64;
  477. mb++;
  478. }
  479. }
  480. if (vs_bit_buffer_damaged && !retried) {
  481. av_log(avctx, AV_LOG_ERROR, "Concealing bitstream errors\n");
  482. retried = 1;
  483. goto retry;
  484. }
  485. /* compute idct and place blocks */
  486. block = &sblock[0][0];
  487. mb = mb_data;
  488. for (mb_index = 0; mb_index < 5; mb_index++) {
  489. dv_calculate_mb_xy(s->sys, s->buf, work_chunk, mb_index, &mb_x, &mb_y);
  490. /* idct_put'ting luminance */
  491. if ((s->sys->pix_fmt == AV_PIX_FMT_YUV420P) ||
  492. (s->sys->pix_fmt == AV_PIX_FMT_YUV411P && mb_x >= (704 / 8)) ||
  493. (s->sys->height >= 720 && mb_y != 134)) {
  494. y_stride = (s->frame->linesize[0] <<
  495. ((!is_field_mode[mb_index]) * log2_blocksize));
  496. } else {
  497. y_stride = (2 << log2_blocksize);
  498. }
  499. y_ptr = s->frame->data[0] +
  500. ((mb_y * s->frame->linesize[0] + mb_x) << log2_blocksize);
  501. if (mb_y == 134 && is_field_mode[mb_index]) {
  502. dv100_idct_put_last_row_field_luma(s, y_ptr, s->frame->linesize[0], block);
  503. } else {
  504. linesize = s->frame->linesize[0] << is_field_mode[mb_index];
  505. mb[0].idct_put(y_ptr, linesize, block + 0 * 64);
  506. if (s->sys->video_stype == 4) { /* SD 422 */
  507. mb[2].idct_put(y_ptr + (1 << log2_blocksize), linesize, block + 2 * 64);
  508. } else {
  509. mb[1].idct_put(y_ptr + (1 << log2_blocksize), linesize, block + 1 * 64);
  510. mb[2].idct_put(y_ptr + y_stride, linesize, block + 2 * 64);
  511. mb[3].idct_put(y_ptr + (1 << log2_blocksize) + y_stride, linesize, block + 3 * 64);
  512. }
  513. }
  514. mb += 4;
  515. block += 4 * 64;
  516. /* idct_put'ting chrominance */
  517. c_offset = (((mb_y >> (s->sys->pix_fmt == AV_PIX_FMT_YUV420P)) * s->frame->linesize[1] +
  518. (mb_x >> ((s->sys->pix_fmt == AV_PIX_FMT_YUV411P) ? 2 : 1))) << log2_blocksize);
  519. for (j = 2; j; j--) {
  520. uint8_t *c_ptr = s->frame->data[j] + c_offset;
  521. if (s->sys->pix_fmt == AV_PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
  522. uint64_t aligned_pixels[64 / 8];
  523. uint8_t *pixels = (uint8_t *) aligned_pixels;
  524. uint8_t *c_ptr1, *ptr1;
  525. int x, y;
  526. mb->idct_put(pixels, 8, block);
  527. for (y = 0; y < (1 << log2_blocksize); y++, c_ptr += s->frame->linesize[j], pixels += 8) {
  528. ptr1 = pixels + ((1 << (log2_blocksize))>>1);
  529. c_ptr1 = c_ptr + (s->frame->linesize[j] << log2_blocksize);
  530. for (x = 0; x < (1 << FFMAX(log2_blocksize - 1, 0)); x++) {
  531. c_ptr[x] = pixels[x];
  532. c_ptr1[x] = ptr1[x];
  533. }
  534. }
  535. block += 64;
  536. mb++;
  537. } else {
  538. y_stride = (mb_y == 134) ? (1 << log2_blocksize) :
  539. s->frame->linesize[j] << ((!is_field_mode[mb_index]) * log2_blocksize);
  540. if (mb_y == 134 && is_field_mode[mb_index]) {
  541. dv100_idct_put_last_row_field_chroma(s, c_ptr, s->frame->linesize[j], block);
  542. mb += 2;
  543. block += 2*64;
  544. } else {
  545. linesize = s->frame->linesize[j] << is_field_mode[mb_index];
  546. (mb++)->idct_put(c_ptr, linesize, block);
  547. block += 64;
  548. if (s->sys->bpm == 8) {
  549. (mb++)->idct_put(c_ptr + y_stride, linesize, block);
  550. block += 64;
  551. }
  552. }
  553. }
  554. }
  555. }
  556. return 0;
  557. }
  558. /* NOTE: exactly one frame must be given (120000 bytes for NTSC,
  559. * 144000 bytes for PAL - or twice those for 50Mbps) */
  560. static int dvvideo_decode_frame(AVCodecContext *avctx, AVFrame *frame,
  561. int *got_frame, AVPacket *avpkt)
  562. {
  563. uint8_t *buf = avpkt->data;
  564. int buf_size = avpkt->size;
  565. DVDecContext *s = avctx->priv_data;
  566. const uint8_t *vsc_pack;
  567. int apt, is16_9, ret;
  568. const AVDVProfile *sys;
  569. sys = ff_dv_frame_profile(avctx, s->sys, buf, buf_size);
  570. if (!sys || buf_size < sys->frame_size) {
  571. av_log(avctx, AV_LOG_ERROR, "could not find dv frame profile\n");
  572. return -1; /* NOTE: we only accept several full frames */
  573. }
  574. if (sys != s->sys) {
  575. ret = ff_dv_init_dynamic_tables(s->work_chunks, sys);
  576. if (ret < 0) {
  577. av_log(avctx, AV_LOG_ERROR, "Error initializing the work tables.\n");
  578. return ret;
  579. }
  580. dv_init_weight_tables(s, sys);
  581. s->sys = sys;
  582. }
  583. s->frame = frame;
  584. frame->flags |= AV_FRAME_FLAG_KEY;
  585. frame->pict_type = AV_PICTURE_TYPE_I;
  586. avctx->pix_fmt = s->sys->pix_fmt;
  587. avctx->framerate = av_inv_q(s->sys->time_base);
  588. avctx->bit_rate = av_rescale_q(s->sys->frame_size,
  589. (AVRational) { 8, 1 },
  590. s->sys->time_base);
  591. ret = ff_set_dimensions(avctx, s->sys->width, s->sys->height);
  592. if (ret < 0)
  593. return ret;
  594. /* Determine the codec's sample_aspect ratio from the packet */
  595. vsc_pack = buf + 80 * 5 + 48 + 5;
  596. if (*vsc_pack == DV_VIDEO_CONTROL) {
  597. apt = buf[4] & 0x07;
  598. is16_9 = (vsc_pack[2] & 0x07) == 0x02 ||
  599. (!apt && (vsc_pack[2] & 0x07) == 0x07);
  600. ff_set_sar(avctx, s->sys->sar[is16_9]);
  601. }
  602. if ((ret = ff_thread_get_buffer(avctx, frame, 0)) < 0)
  603. return ret;
  604. /* Determine the codec's field order from the packet */
  605. if ( *vsc_pack == DV_VIDEO_CONTROL ) {
  606. if (avctx->height == 720) {
  607. frame->flags &= ~AV_FRAME_FLAG_INTERLACED;
  608. frame->flags &= ~AV_FRAME_FLAG_TOP_FIELD_FIRST;
  609. } else if (avctx->height == 1080) {
  610. frame->flags |= AV_FRAME_FLAG_INTERLACED;
  611. frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST * ((vsc_pack[3] & 0x40) == 0x40);
  612. } else {
  613. frame->flags |= AV_FRAME_FLAG_INTERLACED * ((vsc_pack[3] & 0x10) == 0x10);
  614. frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST * !(vsc_pack[3] & 0x40);
  615. }
  616. }
  617. s->buf = buf;
  618. avctx->execute(avctx, dv_decode_video_segment, s->work_chunks, NULL,
  619. dv_work_pool_size(s->sys), sizeof(DVwork_chunk));
  620. emms_c();
  621. /* return image */
  622. *got_frame = 1;
  623. return s->sys->frame_size;
  624. }
  625. const FFCodec ff_dvvideo_decoder = {
  626. .p.name = "dvvideo",
  627. CODEC_LONG_NAME("DV (Digital Video)"),
  628. .p.type = AVMEDIA_TYPE_VIDEO,
  629. .p.id = AV_CODEC_ID_DVVIDEO,
  630. .priv_data_size = sizeof(DVDecContext),
  631. .init = dvvideo_decode_init,
  632. FF_CODEC_DECODE_CB(dvvideo_decode_frame),
  633. .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_SLICE_THREADS,
  634. .p.max_lowres = 3,
  635. };