dv.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  1. /*
  2. * DV decoder
  3. * Copyright (c) 2002 Fabrice Bellard
  4. * Copyright (c) 2004 Roman Shaposhnik
  5. *
  6. * DV encoder
  7. * Copyright (c) 2003 Roman Shaposhnik
  8. *
  9. * 50 Mbps (DVCPRO50) support
  10. * Copyright (c) 2006 Daniel Maas <dmaas@maasdigital.com>
  11. *
  12. * 100 Mbps (DVCPRO HD) support
  13. * Initial code by Daniel Maas <dmaas@maasdigital.com> (funded by BBC R&D)
  14. * Final code by Roman Shaposhnik
  15. *
  16. * Many thanks to Dan Dennedy <dan@dennedy.org> for providing wealth
  17. * of DV technical info.
  18. *
  19. * This file is part of FFmpeg.
  20. *
  21. * FFmpeg is free software; you can redistribute it and/or
  22. * modify it under the terms of the GNU Lesser General Public
  23. * License as published by the Free Software Foundation; either
  24. * version 2.1 of the License, or (at your option) any later version.
  25. *
  26. * FFmpeg is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  29. * Lesser General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU Lesser General Public
  32. * License along with FFmpeg; if not, write to the Free Software
  33. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  34. */
  35. /**
  36. * @file libavcodec/dv.c
  37. * DV codec.
  38. */
  39. #define ALT_BITSTREAM_READER
  40. #include "avcodec.h"
  41. #include "dsputil.h"
  42. #include "bitstream.h"
  43. #include "simple_idct.h"
  44. #include "dvdata.h"
  45. //#undef NDEBUG
  46. //#include <assert.h>
  47. typedef struct DVVideoContext {
  48. const DVprofile *sys;
  49. AVFrame picture;
  50. AVCodecContext *avctx;
  51. uint8_t *buf;
  52. uint8_t dv_zigzag[2][64];
  53. void (*get_pixels)(DCTELEM *block, const uint8_t *pixels, int line_size);
  54. void (*fdct[2])(DCTELEM *block);
  55. void (*idct_put[2])(uint8_t *dest, int line_size, DCTELEM *block);
  56. me_cmp_func ildct_cmp;
  57. } DVVideoContext;
  58. #define TEX_VLC_BITS 9
  59. #if CONFIG_SMALL
  60. #define DV_VLC_MAP_RUN_SIZE 15
  61. #define DV_VLC_MAP_LEV_SIZE 23
  62. #else
  63. #define DV_VLC_MAP_RUN_SIZE 64
  64. #define DV_VLC_MAP_LEV_SIZE 512 //FIXME sign was removed so this should be /2 but needs check
  65. #endif
  66. /* XXX: also include quantization */
  67. static RL_VLC_ELEM dv_rl_vlc[1184];
  68. /* VLC encoding lookup table */
  69. static struct dv_vlc_pair {
  70. uint32_t vlc;
  71. uint8_t size;
  72. } dv_vlc_map[DV_VLC_MAP_RUN_SIZE][DV_VLC_MAP_LEV_SIZE];
  73. static inline int dv_work_pool_size(const DVprofile *d)
  74. {
  75. int size = d->n_difchan*d->difseg_size*27;
  76. if (DV_PROFILE_IS_1080i50(d))
  77. size -= 3*27;
  78. if (DV_PROFILE_IS_720p50(d))
  79. size -= 4*27;
  80. return size;
  81. }
  82. static inline void dv_calc_mb_coordinates(const DVprofile *d, int chan, int seq, int slot,
  83. uint16_t *tbl)
  84. {
  85. static const uint8_t off[] = { 2, 6, 8, 0, 4 };
  86. static const uint8_t shuf1[] = { 36, 18, 54, 0, 72 };
  87. static const uint8_t shuf2[] = { 24, 12, 36, 0, 48 };
  88. static const uint8_t shuf3[] = { 18, 9, 27, 0, 36 };
  89. static const uint8_t l_start[] = {0, 4, 9, 13, 18, 22, 27, 31, 36, 40};
  90. static const uint8_t l_start_shuffled[] = { 9, 4, 13, 0, 18 };
  91. static const uint8_t serpent1[] = {0, 1, 2, 2, 1, 0,
  92. 0, 1, 2, 2, 1, 0,
  93. 0, 1, 2, 2, 1, 0,
  94. 0, 1, 2, 2, 1, 0,
  95. 0, 1, 2};
  96. static const uint8_t serpent2[] = {0, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 0,
  97. 0, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 0,
  98. 0, 1, 2, 3, 4, 5};
  99. static const uint8_t remap[][2] = {{ 0, 0}, { 0, 0}, { 0, 0}, { 0, 0}, /* dummy */
  100. { 0, 0}, { 0, 1}, { 0, 2}, { 0, 3}, {10, 0},
  101. {10, 1}, {10, 2}, {10, 3}, {20, 0}, {20, 1},
  102. {20, 2}, {20, 3}, {30, 0}, {30, 1}, {30, 2},
  103. {30, 3}, {40, 0}, {40, 1}, {40, 2}, {40, 3},
  104. {50, 0}, {50, 1}, {50, 2}, {50, 3}, {60, 0},
  105. {60, 1}, {60, 2}, {60, 3}, {70, 0}, {70, 1},
  106. {70, 2}, {70, 3}, { 0,64}, { 0,65}, { 0,66},
  107. {10,64}, {10,65}, {10,66}, {20,64}, {20,65},
  108. {20,66}, {30,64}, {30,65}, {30,66}, {40,64},
  109. {40,65}, {40,66}, {50,64}, {50,65}, {50,66},
  110. {60,64}, {60,65}, {60,66}, {70,64}, {70,65},
  111. {70,66}, { 0,67}, {20,67}, {40,67}, {60,67}};
  112. int i, k, m;
  113. int x, y, blk;
  114. for (m=0; m<5; m++) {
  115. switch (d->width) {
  116. case 1440:
  117. blk = (chan*11+seq)*27+slot;
  118. if (chan == 0 && seq == 11) {
  119. x = m*27+slot;
  120. if (x<90) {
  121. y = 0;
  122. } else {
  123. x = (x - 90)*2;
  124. y = 67;
  125. }
  126. } else {
  127. i = (4*chan + blk + off[m])%11;
  128. k = (blk/11)%27;
  129. x = shuf1[m] + (chan&1)*9 + k%9;
  130. y = (i*3+k/9)*2 + (chan>>1) + 1;
  131. }
  132. tbl[m] = (x<<1)|(y<<9);
  133. break;
  134. case 1280:
  135. blk = (chan*10+seq)*27+slot;
  136. i = (4*chan + (seq/5) + 2*blk + off[m])%10;
  137. k = (blk/5)%27;
  138. x = shuf1[m]+(chan&1)*9 + k%9;
  139. y = (i*3+k/9)*2 + (chan>>1) + 4;
  140. if (x >= 80) {
  141. x = remap[y][0]+((x-80)<<(y>59));
  142. y = remap[y][1];
  143. }
  144. tbl[m] = (x<<1)|(y<<9);
  145. break;
  146. case 960:
  147. blk = (chan*10+seq)*27+slot;
  148. i = (4*chan + (seq/5) + 2*blk + off[m])%10;
  149. k = (blk/5)%27 + (i&1)*3;
  150. x = shuf2[m] + k%6 + 6*(chan&1);
  151. y = l_start[i] + k/6 + 45*(chan>>1);
  152. tbl[m] = (x<<1)|(y<<9);
  153. break;
  154. case 720:
  155. switch (d->pix_fmt) {
  156. case PIX_FMT_YUV422P:
  157. x = shuf3[m] + slot/3;
  158. y = serpent1[slot] +
  159. ((((seq + off[m]) % d->difseg_size)<<1) + chan)*3;
  160. tbl[m] = (x<<1)|(y<<8);
  161. break;
  162. case PIX_FMT_YUV420P:
  163. x = shuf3[m] + slot/3;
  164. y = serpent1[slot] +
  165. ((seq + off[m]) % d->difseg_size)*3;
  166. tbl[m] = (x<<1)|(y<<9);
  167. break;
  168. case PIX_FMT_YUV411P:
  169. i = (seq + off[m]) % d->difseg_size;
  170. k = slot + ((m==1||m==2)?3:0);
  171. x = l_start_shuffled[m] + k/6;
  172. y = serpent2[k] + i*6;
  173. if (x>21)
  174. y = y*2 - i*6;
  175. tbl[m] = (x<<2)|(y<<8);
  176. break;
  177. }
  178. default:
  179. break;
  180. }
  181. }
  182. }
  183. static int dv_init_dynamic_tables(const DVprofile *d)
  184. {
  185. int j,i,c,s,p;
  186. uint32_t *factor1, *factor2;
  187. const int *iweight1, *iweight2;
  188. if (!d->work_chunks[dv_work_pool_size(d)-1].buf_offset) {
  189. p = i = 0;
  190. for (c=0; c<d->n_difchan; c++) {
  191. for (s=0; s<d->difseg_size; s++) {
  192. p += 6;
  193. for (j=0; j<27; j++) {
  194. p += !(j%3);
  195. if (!(DV_PROFILE_IS_1080i50(d) && c != 0 && s == 11) &&
  196. !(DV_PROFILE_IS_720p50(d) && s > 9)) {
  197. dv_calc_mb_coordinates(d, c, s, j, &d->work_chunks[i].mb_coordinates[0]);
  198. d->work_chunks[i++].buf_offset = p;
  199. }
  200. p += 5;
  201. }
  202. }
  203. }
  204. }
  205. if (!d->idct_factor[DV_PROFILE_IS_HD(d)?8191:5631]) {
  206. factor1 = &d->idct_factor[0];
  207. factor2 = &d->idct_factor[DV_PROFILE_IS_HD(d)?4096:2816];
  208. if (d->height == 720) {
  209. iweight1 = &dv_iweight_720_y[0];
  210. iweight2 = &dv_iweight_720_c[0];
  211. } else {
  212. iweight1 = &dv_iweight_1080_y[0];
  213. iweight2 = &dv_iweight_1080_c[0];
  214. }
  215. if (DV_PROFILE_IS_HD(d)) {
  216. for (c = 0; c < 4; c++) {
  217. for (s = 0; s < 16; s++) {
  218. for (i = 0; i < 64; i++) {
  219. *factor1++ = (dv100_qstep[s] << (c + 9)) * iweight1[i];
  220. *factor2++ = (dv100_qstep[s] << (c + 9)) * iweight2[i];
  221. }
  222. }
  223. }
  224. } else {
  225. iweight1 = &dv_iweight_88[0];
  226. for (j = 0; j < 2; j++, iweight1 = &dv_iweight_248[0]) {
  227. for (s = 0; s < 22; s++) {
  228. for (i = c = 0; c < 4; c++) {
  229. for (; i < dv_quant_areas[c]; i++) {
  230. *factor1 = iweight1[i] << (dv_quant_shifts[s][c] + 1);
  231. *factor2++ = (*factor1++) << 1;
  232. }
  233. }
  234. }
  235. }
  236. }
  237. }
  238. return 0;
  239. }
  240. static av_cold int dvvideo_init(AVCodecContext *avctx)
  241. {
  242. DVVideoContext *s = avctx->priv_data;
  243. DSPContext dsp;
  244. static int done = 0;
  245. int i, j;
  246. if (!done) {
  247. VLC dv_vlc;
  248. uint16_t new_dv_vlc_bits[NB_DV_VLC*2];
  249. uint8_t new_dv_vlc_len[NB_DV_VLC*2];
  250. uint8_t new_dv_vlc_run[NB_DV_VLC*2];
  251. int16_t new_dv_vlc_level[NB_DV_VLC*2];
  252. done = 1;
  253. /* it's faster to include sign bit in a generic VLC parsing scheme */
  254. for (i = 0, j = 0; i < NB_DV_VLC; i++, j++) {
  255. new_dv_vlc_bits[j] = dv_vlc_bits[i];
  256. new_dv_vlc_len[j] = dv_vlc_len[i];
  257. new_dv_vlc_run[j] = dv_vlc_run[i];
  258. new_dv_vlc_level[j] = dv_vlc_level[i];
  259. if (dv_vlc_level[i]) {
  260. new_dv_vlc_bits[j] <<= 1;
  261. new_dv_vlc_len[j]++;
  262. j++;
  263. new_dv_vlc_bits[j] = (dv_vlc_bits[i] << 1) | 1;
  264. new_dv_vlc_len[j] = dv_vlc_len[i] + 1;
  265. new_dv_vlc_run[j] = dv_vlc_run[i];
  266. new_dv_vlc_level[j] = -dv_vlc_level[i];
  267. }
  268. }
  269. /* NOTE: as a trick, we use the fact the no codes are unused
  270. to accelerate the parsing of partial codes */
  271. init_vlc(&dv_vlc, TEX_VLC_BITS, j,
  272. new_dv_vlc_len, 1, 1, new_dv_vlc_bits, 2, 2, 0);
  273. assert(dv_vlc.table_size == 1184);
  274. for (i = 0; i < dv_vlc.table_size; i++){
  275. int code = dv_vlc.table[i][0];
  276. int len = dv_vlc.table[i][1];
  277. int level, run;
  278. if (len < 0){ //more bits needed
  279. run = 0;
  280. level = code;
  281. } else {
  282. run = new_dv_vlc_run [code] + 1;
  283. level = new_dv_vlc_level[code];
  284. }
  285. dv_rl_vlc[i].len = len;
  286. dv_rl_vlc[i].level = level;
  287. dv_rl_vlc[i].run = run;
  288. }
  289. free_vlc(&dv_vlc);
  290. for (i = 0; i < NB_DV_VLC - 1; i++) {
  291. if (dv_vlc_run[i] >= DV_VLC_MAP_RUN_SIZE)
  292. continue;
  293. #if CONFIG_SMALL
  294. if (dv_vlc_level[i] >= DV_VLC_MAP_LEV_SIZE)
  295. continue;
  296. #endif
  297. if (dv_vlc_map[dv_vlc_run[i]][dv_vlc_level[i]].size != 0)
  298. continue;
  299. dv_vlc_map[dv_vlc_run[i]][dv_vlc_level[i]].vlc =
  300. dv_vlc_bits[i] << (!!dv_vlc_level[i]);
  301. dv_vlc_map[dv_vlc_run[i]][dv_vlc_level[i]].size =
  302. dv_vlc_len[i] + (!!dv_vlc_level[i]);
  303. }
  304. for (i = 0; i < DV_VLC_MAP_RUN_SIZE; i++) {
  305. #if CONFIG_SMALL
  306. for (j = 1; j < DV_VLC_MAP_LEV_SIZE; j++) {
  307. if (dv_vlc_map[i][j].size == 0) {
  308. dv_vlc_map[i][j].vlc = dv_vlc_map[0][j].vlc |
  309. (dv_vlc_map[i-1][0].vlc << (dv_vlc_map[0][j].size));
  310. dv_vlc_map[i][j].size = dv_vlc_map[i-1][0].size +
  311. dv_vlc_map[0][j].size;
  312. }
  313. }
  314. #else
  315. for (j = 1; j < DV_VLC_MAP_LEV_SIZE/2; j++) {
  316. if (dv_vlc_map[i][j].size == 0) {
  317. dv_vlc_map[i][j].vlc = dv_vlc_map[0][j].vlc |
  318. (dv_vlc_map[i-1][0].vlc << (dv_vlc_map[0][j].size));
  319. dv_vlc_map[i][j].size = dv_vlc_map[i-1][0].size +
  320. dv_vlc_map[0][j].size;
  321. }
  322. dv_vlc_map[i][((uint16_t)(-j))&0x1ff].vlc =
  323. dv_vlc_map[i][j].vlc | 1;
  324. dv_vlc_map[i][((uint16_t)(-j))&0x1ff].size =
  325. dv_vlc_map[i][j].size;
  326. }
  327. #endif
  328. }
  329. }
  330. /* Generic DSP setup */
  331. dsputil_init(&dsp, avctx);
  332. ff_set_cmp(&dsp, dsp.ildct_cmp, avctx->ildct_cmp);
  333. s->get_pixels = dsp.get_pixels;
  334. s->ildct_cmp = dsp.ildct_cmp[5];
  335. /* 88DCT setup */
  336. s->fdct[0] = dsp.fdct;
  337. s->idct_put[0] = dsp.idct_put;
  338. for (i = 0; i < 64; i++)
  339. s->dv_zigzag[0][i] = dsp.idct_permutation[ff_zigzag_direct[i]];
  340. /* 248DCT setup */
  341. s->fdct[1] = dsp.fdct248;
  342. s->idct_put[1] = ff_simple_idct248_put; // FIXME: need to add it to DSP
  343. if (avctx->lowres){
  344. for (i = 0; i < 64; i++){
  345. int j = ff_zigzag248_direct[i];
  346. s->dv_zigzag[1][i] = dsp.idct_permutation[(j & 7) + (j & 8) * 4 + (j & 48) / 2];
  347. }
  348. }else
  349. memcpy(s->dv_zigzag[1], ff_zigzag248_direct, 64);
  350. avctx->coded_frame = &s->picture;
  351. s->avctx = avctx;
  352. return 0;
  353. }
  354. // #define VLC_DEBUG
  355. // #define printf(...) av_log(NULL, AV_LOG_ERROR, __VA_ARGS__)
  356. typedef struct BlockInfo {
  357. const uint32_t *factor_table;
  358. const uint8_t *scan_table;
  359. uint8_t pos; /* position in block */
  360. void (*idct_put)(uint8_t *dest, int line_size, DCTELEM *block);
  361. uint8_t partial_bit_count;
  362. uint16_t partial_bit_buffer;
  363. int shift_offset;
  364. } BlockInfo;
  365. /* bit budget for AC only in 5 MBs */
  366. static const int vs_total_ac_bits = (100 * 4 + 68*2) * 5;
  367. /* see dv_88_areas and dv_248_areas for details */
  368. static const int mb_area_start[5] = { 1, 6, 21, 43, 64 };
  369. static inline int put_bits_left(PutBitContext* s)
  370. {
  371. return (s->buf_end - s->buf) * 8 - put_bits_count(s);
  372. }
  373. /* decode ac coefficients */
  374. static void dv_decode_ac(GetBitContext *gb, BlockInfo *mb, DCTELEM *block)
  375. {
  376. int last_index = gb->size_in_bits;
  377. const uint8_t *scan_table = mb->scan_table;
  378. const uint32_t *factor_table = mb->factor_table;
  379. int pos = mb->pos;
  380. int partial_bit_count = mb->partial_bit_count;
  381. int level, run, vlc_len, index;
  382. OPEN_READER(re, gb);
  383. UPDATE_CACHE(re, gb);
  384. /* if we must parse a partial vlc, we do it here */
  385. if (partial_bit_count > 0) {
  386. re_cache = ((unsigned)re_cache >> partial_bit_count) |
  387. (mb->partial_bit_buffer << (sizeof(re_cache) * 8 - partial_bit_count));
  388. re_index -= partial_bit_count;
  389. mb->partial_bit_count = 0;
  390. }
  391. /* get the AC coefficients until last_index is reached */
  392. for (;;) {
  393. #ifdef VLC_DEBUG
  394. printf("%2d: bits=%04x index=%d\n", pos, SHOW_UBITS(re, gb, 16), re_index);
  395. #endif
  396. /* our own optimized GET_RL_VLC */
  397. index = NEG_USR32(re_cache, TEX_VLC_BITS);
  398. vlc_len = dv_rl_vlc[index].len;
  399. if (vlc_len < 0) {
  400. index = NEG_USR32((unsigned)re_cache << TEX_VLC_BITS, -vlc_len) + dv_rl_vlc[index].level;
  401. vlc_len = TEX_VLC_BITS - vlc_len;
  402. }
  403. level = dv_rl_vlc[index].level;
  404. run = dv_rl_vlc[index].run;
  405. /* gotta check if we're still within gb boundaries */
  406. if (re_index + vlc_len > last_index) {
  407. /* should be < 16 bits otherwise a codeword could have been parsed */
  408. mb->partial_bit_count = last_index - re_index;
  409. mb->partial_bit_buffer = NEG_USR32(re_cache, mb->partial_bit_count);
  410. re_index = last_index;
  411. break;
  412. }
  413. re_index += vlc_len;
  414. #ifdef VLC_DEBUG
  415. printf("run=%d level=%d\n", run, level);
  416. #endif
  417. pos += run;
  418. if (pos >= 64)
  419. break;
  420. level = (level * factor_table[pos] + (1 << (dv_iweight_bits - 1))) >> dv_iweight_bits;
  421. block[scan_table[pos]] = level;
  422. UPDATE_CACHE(re, gb);
  423. }
  424. CLOSE_READER(re, gb);
  425. mb->pos = pos;
  426. }
  427. static inline void bit_copy(PutBitContext *pb, GetBitContext *gb)
  428. {
  429. int bits_left = get_bits_left(gb);
  430. while (bits_left >= MIN_CACHE_BITS) {
  431. put_bits(pb, MIN_CACHE_BITS, get_bits(gb, MIN_CACHE_BITS));
  432. bits_left -= MIN_CACHE_BITS;
  433. }
  434. if (bits_left > 0) {
  435. put_bits(pb, bits_left, get_bits(gb, bits_left));
  436. }
  437. }
  438. static inline void dv_calculate_mb_xy(DVVideoContext *s, DVwork_chunk *work_chunk, int m, int *mb_x, int *mb_y)
  439. {
  440. *mb_x = work_chunk->mb_coordinates[m] & 0xff;
  441. *mb_y = work_chunk->mb_coordinates[m] >> 8;
  442. /* We work with 720p frames split in half. The odd half-frame (chan==2,3) is displaced :-( */
  443. if (s->sys->height == 720 && !(s->buf[1]&0x0C)) {
  444. *mb_y -= (*mb_y>17)?18:-72; /* shifting the Y coordinate down by 72/2 macro blocks */
  445. }
  446. }
  447. /* mb_x and mb_y are in units of 8 pixels */
  448. static int dv_decode_video_segment(AVCodecContext *avctx, void *arg)
  449. {
  450. DVVideoContext *s = avctx->priv_data;
  451. DVwork_chunk *work_chunk = arg;
  452. int quant, dc, dct_mode, class1, j;
  453. int mb_index, mb_x, mb_y, last_index;
  454. int y_stride, linesize;
  455. DCTELEM *block, *block1;
  456. int c_offset;
  457. uint8_t *y_ptr;
  458. const uint8_t *buf_ptr;
  459. PutBitContext pb, vs_pb;
  460. GetBitContext gb;
  461. BlockInfo mb_data[5 * DV_MAX_BPM], *mb, *mb1;
  462. DECLARE_ALIGNED_16(DCTELEM, sblock[5*DV_MAX_BPM][64]);
  463. DECLARE_ALIGNED_8(uint8_t, mb_bit_buffer[80 + 4]); /* allow some slack */
  464. DECLARE_ALIGNED_8(uint8_t, vs_bit_buffer[5 * 80 + 4]); /* allow some slack */
  465. const int log2_blocksize = 3-s->avctx->lowres;
  466. int is_field_mode[5];
  467. assert((((int)mb_bit_buffer) & 7) == 0);
  468. assert((((int)vs_bit_buffer) & 7) == 0);
  469. memset(sblock, 0, sizeof(sblock));
  470. /* pass 1 : read DC and AC coefficients in blocks */
  471. buf_ptr = &s->buf[work_chunk->buf_offset*80];
  472. block1 = &sblock[0][0];
  473. mb1 = mb_data;
  474. init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80);
  475. for (mb_index = 0; mb_index < 5; mb_index++, mb1 += s->sys->bpm, block1 += s->sys->bpm * 64) {
  476. /* skip header */
  477. quant = buf_ptr[3] & 0x0f;
  478. buf_ptr += 4;
  479. init_put_bits(&pb, mb_bit_buffer, 80);
  480. mb = mb1;
  481. block = block1;
  482. is_field_mode[mb_index] = 0;
  483. for (j = 0; j < s->sys->bpm; j++) {
  484. last_index = s->sys->block_sizes[j];
  485. init_get_bits(&gb, buf_ptr, last_index);
  486. /* get the dc */
  487. dc = get_sbits(&gb, 9);
  488. dct_mode = get_bits1(&gb);
  489. class1 = get_bits(&gb, 2);
  490. if (DV_PROFILE_IS_HD(s->sys)) {
  491. mb->idct_put = s->idct_put[0];
  492. mb->scan_table = s->dv_zigzag[0];
  493. mb->factor_table = &s->sys->idct_factor[(j >= 4)*4*16*64 + class1*16*64 + quant*64];
  494. is_field_mode[mb_index] |= !j && dct_mode;
  495. } else {
  496. mb->idct_put = s->idct_put[dct_mode && log2_blocksize == 3];
  497. mb->scan_table = s->dv_zigzag[dct_mode];
  498. mb->factor_table = &s->sys->idct_factor[(class1 == 3)*2*22*64 + dct_mode*22*64 +
  499. (quant + dv_quant_offset[class1])*64];
  500. }
  501. dc = dc << 2;
  502. /* convert to unsigned because 128 is not added in the
  503. standard IDCT */
  504. dc += 1024;
  505. block[0] = dc;
  506. buf_ptr += last_index >> 3;
  507. mb->pos = 0;
  508. mb->partial_bit_count = 0;
  509. #ifdef VLC_DEBUG
  510. printf("MB block: %d, %d ", mb_index, j);
  511. #endif
  512. dv_decode_ac(&gb, mb, block);
  513. /* write the remaining bits in a new buffer only if the
  514. block is finished */
  515. if (mb->pos >= 64)
  516. bit_copy(&pb, &gb);
  517. block += 64;
  518. mb++;
  519. }
  520. /* pass 2 : we can do it just after */
  521. #ifdef VLC_DEBUG
  522. printf("***pass 2 size=%d MB#=%d\n", put_bits_count(&pb), mb_index);
  523. #endif
  524. block = block1;
  525. mb = mb1;
  526. init_get_bits(&gb, mb_bit_buffer, put_bits_count(&pb));
  527. flush_put_bits(&pb);
  528. for (j = 0; j < s->sys->bpm; j++, block += 64, mb++) {
  529. if (mb->pos < 64 && get_bits_left(&gb) > 0) {
  530. dv_decode_ac(&gb, mb, block);
  531. /* if still not finished, no need to parse other blocks */
  532. if (mb->pos < 64)
  533. break;
  534. }
  535. }
  536. /* all blocks are finished, so the extra bytes can be used at
  537. the video segment level */
  538. if (j >= s->sys->bpm)
  539. bit_copy(&vs_pb, &gb);
  540. }
  541. /* we need a pass other the whole video segment */
  542. #ifdef VLC_DEBUG
  543. printf("***pass 3 size=%d\n", put_bits_count(&vs_pb));
  544. #endif
  545. block = &sblock[0][0];
  546. mb = mb_data;
  547. init_get_bits(&gb, vs_bit_buffer, put_bits_count(&vs_pb));
  548. flush_put_bits(&vs_pb);
  549. for (mb_index = 0; mb_index < 5; mb_index++) {
  550. for (j = 0; j < s->sys->bpm; j++) {
  551. if (mb->pos < 64) {
  552. #ifdef VLC_DEBUG
  553. printf("start %d:%d\n", mb_index, j);
  554. #endif
  555. dv_decode_ac(&gb, mb, block);
  556. }
  557. if (mb->pos >= 64 && mb->pos < 127)
  558. av_log(NULL, AV_LOG_ERROR, "AC EOB marker is absent pos=%d\n", mb->pos);
  559. block += 64;
  560. mb++;
  561. }
  562. }
  563. /* compute idct and place blocks */
  564. block = &sblock[0][0];
  565. mb = mb_data;
  566. for (mb_index = 0; mb_index < 5; mb_index++) {
  567. dv_calculate_mb_xy(s, work_chunk, mb_index, &mb_x, &mb_y);
  568. /* idct_put'ting luminance */
  569. if ((s->sys->pix_fmt == PIX_FMT_YUV420P) ||
  570. (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) ||
  571. (s->sys->height >= 720 && mb_y != 134)) {
  572. y_stride = (s->picture.linesize[0] << ((!is_field_mode[mb_index]) * log2_blocksize));
  573. } else {
  574. y_stride = (2 << log2_blocksize);
  575. }
  576. y_ptr = s->picture.data[0] + ((mb_y * s->picture.linesize[0] + mb_x) << log2_blocksize);
  577. linesize = s->picture.linesize[0] << is_field_mode[mb_index];
  578. mb[0] .idct_put(y_ptr , linesize, block + 0*64);
  579. if (s->sys->video_stype == 4) { /* SD 422 */
  580. mb[2].idct_put(y_ptr + (1 << log2_blocksize) , linesize, block + 2*64);
  581. } else {
  582. mb[1].idct_put(y_ptr + (1 << log2_blocksize) , linesize, block + 1*64);
  583. mb[2].idct_put(y_ptr + y_stride, linesize, block + 2*64);
  584. mb[3].idct_put(y_ptr + (1 << log2_blocksize) + y_stride, linesize, block + 3*64);
  585. }
  586. mb += 4;
  587. block += 4*64;
  588. /* idct_put'ting chrominance */
  589. c_offset = (((mb_y >> (s->sys->pix_fmt == PIX_FMT_YUV420P)) * s->picture.linesize[1] +
  590. (mb_x >> ((s->sys->pix_fmt == PIX_FMT_YUV411P) ? 2 : 1))) << log2_blocksize);
  591. for (j = 2; j; j--) {
  592. uint8_t *c_ptr = s->picture.data[j] + c_offset;
  593. if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
  594. uint64_t aligned_pixels[64/8];
  595. uint8_t *pixels = (uint8_t*)aligned_pixels;
  596. uint8_t *c_ptr1, *ptr1;
  597. int x, y;
  598. mb->idct_put(pixels, 8, block);
  599. for (y = 0; y < (1 << log2_blocksize); y++, c_ptr += s->picture.linesize[j], pixels += 8) {
  600. ptr1 = pixels + (1 << (log2_blocksize - 1));
  601. c_ptr1 = c_ptr + (s->picture.linesize[j] << log2_blocksize);
  602. for (x = 0; x < (1 << (log2_blocksize - 1)); x++) {
  603. c_ptr[x] = pixels[x];
  604. c_ptr1[x] = ptr1[x];
  605. }
  606. }
  607. block += 64; mb++;
  608. } else {
  609. y_stride = (mb_y == 134) ? (1 << log2_blocksize) :
  610. s->picture.linesize[j] << ((!is_field_mode[mb_index]) * log2_blocksize);
  611. linesize = s->picture.linesize[j] << is_field_mode[mb_index];
  612. (mb++)-> idct_put(c_ptr , linesize, block); block += 64;
  613. if (s->sys->bpm == 8) {
  614. (mb++)->idct_put(c_ptr + y_stride, linesize, block); block += 64;
  615. }
  616. }
  617. }
  618. }
  619. return 0;
  620. }
  621. #if CONFIG_SMALL
  622. /* Converts run and level (where level != 0) pair into vlc, returning bit size */
  623. static av_always_inline int dv_rl2vlc(int run, int level, int sign, uint32_t* vlc)
  624. {
  625. int size;
  626. if (run < DV_VLC_MAP_RUN_SIZE && level < DV_VLC_MAP_LEV_SIZE) {
  627. *vlc = dv_vlc_map[run][level].vlc | sign;
  628. size = dv_vlc_map[run][level].size;
  629. }
  630. else {
  631. if (level < DV_VLC_MAP_LEV_SIZE) {
  632. *vlc = dv_vlc_map[0][level].vlc | sign;
  633. size = dv_vlc_map[0][level].size;
  634. } else {
  635. *vlc = 0xfe00 | (level << 1) | sign;
  636. size = 16;
  637. }
  638. if (run) {
  639. *vlc |= ((run < 16) ? dv_vlc_map[run-1][0].vlc :
  640. (0x1f80 | (run - 1))) << size;
  641. size += (run < 16) ? dv_vlc_map[run-1][0].size : 13;
  642. }
  643. }
  644. return size;
  645. }
  646. static av_always_inline int dv_rl2vlc_size(int run, int level)
  647. {
  648. int size;
  649. if (run < DV_VLC_MAP_RUN_SIZE && level < DV_VLC_MAP_LEV_SIZE) {
  650. size = dv_vlc_map[run][level].size;
  651. }
  652. else {
  653. size = (level < DV_VLC_MAP_LEV_SIZE) ? dv_vlc_map[0][level].size : 16;
  654. if (run) {
  655. size += (run < 16) ? dv_vlc_map[run-1][0].size : 13;
  656. }
  657. }
  658. return size;
  659. }
  660. #else
  661. static av_always_inline int dv_rl2vlc(int run, int l, int sign, uint32_t* vlc)
  662. {
  663. *vlc = dv_vlc_map[run][l].vlc | sign;
  664. return dv_vlc_map[run][l].size;
  665. }
  666. static av_always_inline int dv_rl2vlc_size(int run, int l)
  667. {
  668. return dv_vlc_map[run][l].size;
  669. }
  670. #endif
  671. typedef struct EncBlockInfo {
  672. int area_q[4];
  673. int bit_size[4];
  674. int prev[5];
  675. int cur_ac;
  676. int cno;
  677. int dct_mode;
  678. DCTELEM mb[64];
  679. uint8_t next[64];
  680. uint8_t sign[64];
  681. uint8_t partial_bit_count;
  682. uint32_t partial_bit_buffer; /* we can't use uint16_t here */
  683. } EncBlockInfo;
  684. static av_always_inline PutBitContext* dv_encode_ac(EncBlockInfo* bi,
  685. PutBitContext* pb_pool,
  686. PutBitContext* pb_end)
  687. {
  688. int prev, bits_left;
  689. PutBitContext* pb = pb_pool;
  690. int size = bi->partial_bit_count;
  691. uint32_t vlc = bi->partial_bit_buffer;
  692. bi->partial_bit_count = bi->partial_bit_buffer = 0;
  693. for (;;){
  694. /* Find suitable storage space */
  695. for (; size > (bits_left = put_bits_left(pb)); pb++) {
  696. if (bits_left) {
  697. size -= bits_left;
  698. put_bits(pb, bits_left, vlc >> size);
  699. vlc = vlc & ((1 << size) - 1);
  700. }
  701. if (pb + 1 >= pb_end) {
  702. bi->partial_bit_count = size;
  703. bi->partial_bit_buffer = vlc;
  704. return pb;
  705. }
  706. }
  707. /* Store VLC */
  708. put_bits(pb, size, vlc);
  709. if (bi->cur_ac >= 64)
  710. break;
  711. /* Construct the next VLC */
  712. prev = bi->cur_ac;
  713. bi->cur_ac = bi->next[prev];
  714. if (bi->cur_ac < 64){
  715. size = dv_rl2vlc(bi->cur_ac - prev - 1, bi->mb[bi->cur_ac], bi->sign[bi->cur_ac], &vlc);
  716. } else {
  717. size = 4; vlc = 6; /* End Of Block stamp */
  718. }
  719. }
  720. return pb;
  721. }
  722. static av_always_inline int dv_guess_dct_mode(DVVideoContext *s, uint8_t *data, int linesize) {
  723. if (s->avctx->flags & CODEC_FLAG_INTERLACED_DCT) {
  724. int ps = s->ildct_cmp(NULL, data, NULL, linesize, 8) - 400;
  725. if (ps > 0) {
  726. int is = s->ildct_cmp(NULL, data , NULL, linesize<<1, 4) +
  727. s->ildct_cmp(NULL, data + linesize, NULL, linesize<<1, 4);
  728. return (ps > is);
  729. }
  730. }
  731. return 0;
  732. }
  733. static av_always_inline int dv_init_enc_block(EncBlockInfo* bi, uint8_t *data, int linesize, DVVideoContext *s, int bias)
  734. {
  735. const int *weight;
  736. const uint8_t* zigzag_scan;
  737. DECLARE_ALIGNED_16(DCTELEM, blk[64]);
  738. int i, area;
  739. /* We offer two different methods for class number assignment: the
  740. method suggested in SMPTE 314M Table 22, and an improved
  741. method. The SMPTE method is very conservative; it assigns class
  742. 3 (i.e. severe quantization) to any block where the largest AC
  743. component is greater than 36. FFmpeg's DV encoder tracks AC bit
  744. consumption precisely, so there is no need to bias most blocks
  745. towards strongly lossy compression. Instead, we assign class 2
  746. to most blocks, and use class 3 only when strictly necessary
  747. (for blocks whose largest AC component exceeds 255). */
  748. #if 0 /* SMPTE spec method */
  749. static const int classes[] = {12, 24, 36, 0xffff};
  750. #else /* improved FFmpeg method */
  751. static const int classes[] = {-1, -1, 255, 0xffff};
  752. #endif
  753. int max = classes[0];
  754. int prev = 0;
  755. assert((((int)blk) & 15) == 0);
  756. bi->area_q[0] = bi->area_q[1] = bi->area_q[2] = bi->area_q[3] = 0;
  757. bi->partial_bit_count = 0;
  758. bi->partial_bit_buffer = 0;
  759. bi->cur_ac = 0;
  760. if (data) {
  761. bi->dct_mode = dv_guess_dct_mode(s, data, linesize);
  762. s->get_pixels(blk, data, linesize);
  763. s->fdct[bi->dct_mode](blk);
  764. } else {
  765. /* We rely on the fact that encoding all zeros leads to an immediate EOB,
  766. which is precisely what the spec calls for in the "dummy" blocks. */
  767. memset(blk, 0, sizeof(blk));
  768. bi->dct_mode = 0;
  769. }
  770. bi->mb[0] = blk[0];
  771. zigzag_scan = bi->dct_mode ? ff_zigzag248_direct : ff_zigzag_direct;
  772. weight = bi->dct_mode ? dv_weight_248 : dv_weight_88;
  773. for (area = 0; area < 4; area++) {
  774. bi->prev[area] = prev;
  775. bi->bit_size[area] = 1; // 4 areas 4 bits for EOB :)
  776. for (i = mb_area_start[area]; i < mb_area_start[area+1]; i++) {
  777. int level = blk[zigzag_scan[i]];
  778. if (level + 15 > 30U) {
  779. bi->sign[i] = (level >> 31) & 1;
  780. /* weigh it and and shift down into range, adding for rounding */
  781. /* the extra division by a factor of 2^4 reverses the 8x expansion of the DCT
  782. AND the 2x doubling of the weights */
  783. level = (FFABS(level) * weight[i] + (1 << (dv_weight_bits+3))) >> (dv_weight_bits+4);
  784. bi->mb[i] = level;
  785. if (level > max)
  786. max = level;
  787. bi->bit_size[area] += dv_rl2vlc_size(i - prev - 1, level);
  788. bi->next[prev]= i;
  789. prev = i;
  790. }
  791. }
  792. }
  793. bi->next[prev]= i;
  794. for (bi->cno = 0; max > classes[bi->cno]; bi->cno++);
  795. bi->cno += bias;
  796. if (bi->cno >= 3) {
  797. bi->cno = 3;
  798. prev = 0;
  799. i = bi->next[prev];
  800. for (area = 0; area < 4; area++) {
  801. bi->prev[area] = prev;
  802. bi->bit_size[area] = 1; // 4 areas 4 bits for EOB :)
  803. for (; i < mb_area_start[area+1]; i = bi->next[i]) {
  804. bi->mb[i] >>= 1;
  805. if (bi->mb[i]) {
  806. bi->bit_size[area] += dv_rl2vlc_size(i - prev - 1, bi->mb[i]);
  807. bi->next[prev]= i;
  808. prev = i;
  809. }
  810. }
  811. }
  812. bi->next[prev]= i;
  813. }
  814. return bi->bit_size[0] + bi->bit_size[1] + bi->bit_size[2] + bi->bit_size[3];
  815. }
  816. static inline void dv_guess_qnos(EncBlockInfo* blks, int* qnos)
  817. {
  818. int size[5];
  819. int i, j, k, a, prev, a2;
  820. EncBlockInfo* b;
  821. size[0] = size[1] = size[2] = size[3] = size[4] = 1 << 24;
  822. do {
  823. b = blks;
  824. for (i = 0; i < 5; i++) {
  825. if (!qnos[i])
  826. continue;
  827. qnos[i]--;
  828. size[i] = 0;
  829. for (j = 0; j < 6; j++, b++) {
  830. for (a = 0; a < 4; a++) {
  831. if (b->area_q[a] != dv_quant_shifts[qnos[i] + dv_quant_offset[b->cno]][a]) {
  832. b->bit_size[a] = 1; // 4 areas 4 bits for EOB :)
  833. b->area_q[a]++;
  834. prev = b->prev[a];
  835. assert(b->next[prev] >= mb_area_start[a+1] || b->mb[prev]);
  836. for (k = b->next[prev] ; k < mb_area_start[a+1]; k = b->next[k]) {
  837. b->mb[k] >>= 1;
  838. if (b->mb[k]) {
  839. b->bit_size[a] += dv_rl2vlc_size(k - prev - 1, b->mb[k]);
  840. prev = k;
  841. } else {
  842. if (b->next[k] >= mb_area_start[a+1] && b->next[k]<64){
  843. for (a2 = a + 1; b->next[k] >= mb_area_start[a2+1]; a2++)
  844. b->prev[a2] = prev;
  845. assert(a2 < 4);
  846. assert(b->mb[b->next[k]]);
  847. b->bit_size[a2] += dv_rl2vlc_size(b->next[k] - prev - 1, b->mb[b->next[k]])
  848. -dv_rl2vlc_size(b->next[k] - k - 1, b->mb[b->next[k]]);
  849. assert(b->prev[a2] == k && (a2 + 1 >= 4 || b->prev[a2+1] != k));
  850. b->prev[a2] = prev;
  851. }
  852. b->next[prev] = b->next[k];
  853. }
  854. }
  855. b->prev[a+1]= prev;
  856. }
  857. size[i] += b->bit_size[a];
  858. }
  859. }
  860. if (vs_total_ac_bits >= size[0] + size[1] + size[2] + size[3] + size[4])
  861. return;
  862. }
  863. } while (qnos[0]|qnos[1]|qnos[2]|qnos[3]|qnos[4]);
  864. for (a = 2; a == 2 || vs_total_ac_bits < size[0]; a += a){
  865. b = blks;
  866. size[0] = 5 * 6 * 4; //EOB
  867. for (j = 0; j < 6 *5; j++, b++) {
  868. prev = b->prev[0];
  869. for (k = b->next[prev]; k < 64; k = b->next[k]) {
  870. if (b->mb[k] < a && b->mb[k] > -a){
  871. b->next[prev] = b->next[k];
  872. }else{
  873. size[0] += dv_rl2vlc_size(k - prev - 1, b->mb[k]);
  874. prev = k;
  875. }
  876. }
  877. }
  878. }
  879. }
  880. static int dv_encode_video_segment(AVCodecContext *avctx, void *arg)
  881. {
  882. DVVideoContext *s = avctx->priv_data;
  883. DVwork_chunk *work_chunk = arg;
  884. int mb_index, i, j;
  885. int mb_x, mb_y, c_offset, linesize, y_stride;
  886. uint8_t* y_ptr;
  887. uint8_t* dif;
  888. uint8_t scratch[64];
  889. EncBlockInfo enc_blks[5*DV_MAX_BPM];
  890. PutBitContext pbs[5*DV_MAX_BPM];
  891. PutBitContext* pb;
  892. EncBlockInfo* enc_blk;
  893. int vs_bit_size = 0;
  894. int qnos[5] = {15, 15, 15, 15, 15}; /* No quantization */
  895. int* qnosp = &qnos[0];
  896. dif = &s->buf[work_chunk->buf_offset*80];
  897. enc_blk = &enc_blks[0];
  898. for (mb_index = 0; mb_index < 5; mb_index++) {
  899. dv_calculate_mb_xy(s, work_chunk, mb_index, &mb_x, &mb_y);
  900. /* initializing luminance blocks */
  901. if ((s->sys->pix_fmt == PIX_FMT_YUV420P) ||
  902. (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) ||
  903. (s->sys->height >= 720 && mb_y != 134)) {
  904. y_stride = s->picture.linesize[0] << 3;
  905. } else {
  906. y_stride = 16;
  907. }
  908. y_ptr = s->picture.data[0] + ((mb_y * s->picture.linesize[0] + mb_x) << 3);
  909. linesize = s->picture.linesize[0];
  910. if (s->sys->video_stype == 4) { /* SD 422 */
  911. vs_bit_size +=
  912. dv_init_enc_block(enc_blk+0, y_ptr , linesize, s, 0) +
  913. dv_init_enc_block(enc_blk+1, NULL , linesize, s, 0) +
  914. dv_init_enc_block(enc_blk+2, y_ptr + 8 , linesize, s, 0) +
  915. dv_init_enc_block(enc_blk+3, NULL , linesize, s, 0);
  916. } else {
  917. vs_bit_size +=
  918. dv_init_enc_block(enc_blk+0, y_ptr , linesize, s, 0) +
  919. dv_init_enc_block(enc_blk+1, y_ptr + 8 , linesize, s, 0) +
  920. dv_init_enc_block(enc_blk+2, y_ptr + y_stride, linesize, s, 0) +
  921. dv_init_enc_block(enc_blk+3, y_ptr + 8 + y_stride, linesize, s, 0);
  922. }
  923. enc_blk += 4;
  924. /* initializing chrominance blocks */
  925. c_offset = (((mb_y >> (s->sys->pix_fmt == PIX_FMT_YUV420P)) * s->picture.linesize[1] +
  926. (mb_x >> ((s->sys->pix_fmt == PIX_FMT_YUV411P) ? 2 : 1))) << 3);
  927. for (j = 2; j; j--) {
  928. uint8_t *c_ptr = s->picture.data[j] + c_offset;
  929. linesize = s->picture.linesize[j];
  930. y_stride = (mb_y == 134) ? 8 : (s->picture.linesize[j] << 3);
  931. if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
  932. uint8_t* d;
  933. uint8_t* b = scratch;
  934. for (i = 0; i < 8; i++) {
  935. d = c_ptr + (linesize << 3);
  936. b[0] = c_ptr[0]; b[1] = c_ptr[1]; b[2] = c_ptr[2]; b[3] = c_ptr[3];
  937. b[4] = d[0]; b[5] = d[1]; b[6] = d[2]; b[7] = d[3];
  938. c_ptr += linesize;
  939. b += 8;
  940. }
  941. c_ptr = scratch;
  942. linesize = 8;
  943. }
  944. vs_bit_size += dv_init_enc_block( enc_blk++, c_ptr , linesize, s, 1);
  945. if (s->sys->bpm == 8) {
  946. vs_bit_size += dv_init_enc_block(enc_blk++, c_ptr + y_stride, linesize, s, 1);
  947. }
  948. }
  949. }
  950. if (vs_total_ac_bits < vs_bit_size)
  951. dv_guess_qnos(&enc_blks[0], qnosp);
  952. /* DIF encoding process */
  953. for (j=0; j<5*s->sys->bpm;) {
  954. int start_mb = j;
  955. dif[3] = *qnosp++;
  956. dif += 4;
  957. /* First pass over individual cells only */
  958. for (i=0; i<s->sys->bpm; i++, j++) {
  959. int sz = s->sys->block_sizes[i]>>3;
  960. init_put_bits(&pbs[j], dif, sz);
  961. put_bits(&pbs[j], 9, (uint16_t)(((enc_blks[j].mb[0] >> 3) - 1024 + 2) >> 2));
  962. put_bits(&pbs[j], 1, enc_blks[j].dct_mode);
  963. put_bits(&pbs[j], 2, enc_blks[j].cno);
  964. dv_encode_ac(&enc_blks[j], &pbs[j], &pbs[j+1]);
  965. dif += sz;
  966. }
  967. /* Second pass over each MB space */
  968. pb = &pbs[start_mb];
  969. for (i=0; i<s->sys->bpm; i++) {
  970. if (enc_blks[start_mb+i].partial_bit_count)
  971. pb = dv_encode_ac(&enc_blks[start_mb+i], pb, &pbs[start_mb+s->sys->bpm]);
  972. }
  973. }
  974. /* Third and final pass over the whole video segment space */
  975. pb = &pbs[0];
  976. for (j=0; j<5*s->sys->bpm; j++) {
  977. if (enc_blks[j].partial_bit_count)
  978. pb = dv_encode_ac(&enc_blks[j], pb, &pbs[s->sys->bpm*5]);
  979. if (enc_blks[j].partial_bit_count)
  980. av_log(NULL, AV_LOG_ERROR, "ac bitstream overflow\n");
  981. }
  982. for (j=0; j<5*s->sys->bpm; j++)
  983. flush_put_bits(&pbs[j]);
  984. return 0;
  985. }
  986. #if CONFIG_DVVIDEO_DECODER
  987. /* NOTE: exactly one frame must be given (120000 bytes for NTSC,
  988. 144000 bytes for PAL - or twice those for 50Mbps) */
  989. static int dvvideo_decode_frame(AVCodecContext *avctx,
  990. void *data, int *data_size,
  991. const uint8_t *buf, int buf_size)
  992. {
  993. DVVideoContext *s = avctx->priv_data;
  994. s->sys = dv_frame_profile(buf);
  995. if (!s->sys || buf_size < s->sys->frame_size || dv_init_dynamic_tables(s->sys))
  996. return -1; /* NOTE: we only accept several full frames */
  997. if (s->picture.data[0])
  998. avctx->release_buffer(avctx, &s->picture);
  999. s->picture.reference = 0;
  1000. s->picture.key_frame = 1;
  1001. s->picture.pict_type = FF_I_TYPE;
  1002. avctx->pix_fmt = s->sys->pix_fmt;
  1003. avctx->time_base = s->sys->time_base;
  1004. avcodec_set_dimensions(avctx, s->sys->width, s->sys->height);
  1005. if (avctx->get_buffer(avctx, &s->picture) < 0) {
  1006. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1007. return -1;
  1008. }
  1009. s->picture.interlaced_frame = 1;
  1010. s->picture.top_field_first = 0;
  1011. s->buf = buf;
  1012. avctx->execute(avctx, dv_decode_video_segment, s->sys->work_chunks, NULL,
  1013. dv_work_pool_size(s->sys), sizeof(DVwork_chunk));
  1014. emms_c();
  1015. /* return image */
  1016. *data_size = sizeof(AVFrame);
  1017. *(AVFrame*)data = s->picture;
  1018. return s->sys->frame_size;
  1019. }
  1020. #endif /* CONFIG_DVVIDEO_DECODER */
  1021. static inline int dv_write_pack(enum dv_pack_type pack_id, DVVideoContext *c,
  1022. uint8_t* buf)
  1023. {
  1024. /*
  1025. * Here's what SMPTE314M says about these two:
  1026. * (page 6) APTn, AP1n, AP2n, AP3n: These data shall be identical
  1027. * as track application IDs (APTn = 001, AP1n =
  1028. * 001, AP2n = 001, AP3n = 001), if the source signal
  1029. * comes from a digital VCR. If the signal source is
  1030. * unknown, all bits for these data shall be set to 1.
  1031. * (page 12) STYPE: STYPE defines a signal type of video signal
  1032. * 00000b = 4:1:1 compression
  1033. * 00100b = 4:2:2 compression
  1034. * XXXXXX = Reserved
  1035. * Now, I've got two problems with these statements:
  1036. * 1. it looks like APT == 111b should be a safe bet, but it isn't.
  1037. * It seems that for PAL as defined in IEC 61834 we have to set
  1038. * APT to 000 and for SMPTE314M to 001.
  1039. * 2. It is not at all clear what STYPE is used for 4:2:0 PAL
  1040. * compression scheme (if any).
  1041. */
  1042. int apt = (c->sys->pix_fmt == PIX_FMT_YUV420P ? 0 : 1);
  1043. uint8_t aspect = 0;
  1044. if ((int)(av_q2d(c->avctx->sample_aspect_ratio) * c->avctx->width / c->avctx->height * 10) >= 17) /* 16:9 */
  1045. aspect = 0x02;
  1046. buf[0] = (uint8_t)pack_id;
  1047. switch (pack_id) {
  1048. case dv_header525: /* I can't imagine why these two weren't defined as real */
  1049. case dv_header625: /* packs in SMPTE314M -- they definitely look like ones */
  1050. buf[1] = 0xf8 | /* reserved -- always 1 */
  1051. (apt & 0x07); /* APT: Track application ID */
  1052. buf[2] = (0 << 7) | /* TF1: audio data is 0 - valid; 1 - invalid */
  1053. (0x0f << 3) | /* reserved -- always 1 */
  1054. (apt & 0x07); /* AP1: Audio application ID */
  1055. buf[3] = (0 << 7) | /* TF2: video data is 0 - valid; 1 - invalid */
  1056. (0x0f << 3) | /* reserved -- always 1 */
  1057. (apt & 0x07); /* AP2: Video application ID */
  1058. buf[4] = (0 << 7) | /* TF3: subcode(SSYB) is 0 - valid; 1 - invalid */
  1059. (0x0f << 3) | /* reserved -- always 1 */
  1060. (apt & 0x07); /* AP3: Subcode application ID */
  1061. break;
  1062. case dv_video_source:
  1063. buf[1] = 0xff; /* reserved -- always 1 */
  1064. buf[2] = (1 << 7) | /* B/W: 0 - b/w, 1 - color */
  1065. (1 << 6) | /* following CLF is valid - 0, invalid - 1 */
  1066. (3 << 4) | /* CLF: color frames ID (see ITU-R BT.470-4) */
  1067. 0xf; /* reserved -- always 1 */
  1068. buf[3] = (3 << 6) | /* reserved -- always 1 */
  1069. (c->sys->dsf << 5) | /* system: 60fields/50fields */
  1070. c->sys->video_stype; /* signal type video compression */
  1071. buf[4] = 0xff; /* VISC: 0xff -- no information */
  1072. break;
  1073. case dv_video_control:
  1074. buf[1] = (0 << 6) | /* Copy generation management (CGMS) 0 -- free */
  1075. 0x3f; /* reserved -- always 1 */
  1076. buf[2] = 0xc8 | /* reserved -- always b11001xxx */
  1077. aspect;
  1078. buf[3] = (1 << 7) | /* frame/field flag 1 -- frame, 0 -- field */
  1079. (1 << 6) | /* first/second field flag 0 -- field 2, 1 -- field 1 */
  1080. (1 << 5) | /* frame change flag 0 -- same picture as before, 1 -- different */
  1081. (1 << 4) | /* 1 - interlaced, 0 - noninterlaced */
  1082. 0xc; /* reserved -- always b1100 */
  1083. buf[4] = 0xff; /* reserved -- always 1 */
  1084. break;
  1085. default:
  1086. buf[1] = buf[2] = buf[3] = buf[4] = 0xff;
  1087. }
  1088. return 5;
  1089. }
  1090. #if CONFIG_DVVIDEO_ENCODER
  1091. static void dv_format_frame(DVVideoContext* c, uint8_t* buf)
  1092. {
  1093. int chan, i, j, k;
  1094. for (chan = 0; chan < c->sys->n_difchan; chan++) {
  1095. for (i = 0; i < c->sys->difseg_size; i++) {
  1096. memset(buf, 0xff, 80 * 6); /* first 6 DIF blocks are for control data */
  1097. /* DV header: 1DIF */
  1098. buf += dv_write_dif_id(dv_sect_header, chan, i, 0, buf);
  1099. buf += dv_write_pack((c->sys->dsf ? dv_header625 : dv_header525), c, buf);
  1100. buf += 72; /* unused bytes */
  1101. /* DV subcode: 2DIFs */
  1102. for (j = 0; j < 2; j++) {
  1103. buf += dv_write_dif_id(dv_sect_subcode, chan, i, j, buf);
  1104. for (k = 0; k < 6; k++)
  1105. buf += dv_write_ssyb_id(k, (i < c->sys->difseg_size/2), buf) + 5;
  1106. buf += 29; /* unused bytes */
  1107. }
  1108. /* DV VAUX: 3DIFS */
  1109. for (j = 0; j < 3; j++) {
  1110. buf += dv_write_dif_id(dv_sect_vaux, chan, i, j, buf);
  1111. buf += dv_write_pack(dv_video_source, c, buf);
  1112. buf += dv_write_pack(dv_video_control, c, buf);
  1113. buf += 7*5;
  1114. buf += dv_write_pack(dv_video_source, c, buf);
  1115. buf += dv_write_pack(dv_video_control, c, buf);
  1116. buf += 4*5 + 2; /* unused bytes */
  1117. }
  1118. /* DV Audio/Video: 135 Video DIFs + 9 Audio DIFs */
  1119. for (j = 0; j < 135; j++) {
  1120. if (j%15 == 0) {
  1121. memset(buf, 0xff, 80);
  1122. buf += dv_write_dif_id(dv_sect_audio, chan, i, j/15, buf);
  1123. buf += 77; /* audio control & shuffled PCM audio */
  1124. }
  1125. buf += dv_write_dif_id(dv_sect_video, chan, i, j, buf);
  1126. buf += 77; /* 1 video macroblock: 1 bytes control
  1127. 4 * 14 bytes Y 8x8 data
  1128. 10 bytes Cr 8x8 data
  1129. 10 bytes Cb 8x8 data */
  1130. }
  1131. }
  1132. }
  1133. }
  1134. static int dvvideo_encode_frame(AVCodecContext *c, uint8_t *buf, int buf_size,
  1135. void *data)
  1136. {
  1137. DVVideoContext *s = c->priv_data;
  1138. s->sys = dv_codec_profile(c);
  1139. if (!s->sys || buf_size < s->sys->frame_size || dv_init_dynamic_tables(s->sys))
  1140. return -1;
  1141. c->pix_fmt = s->sys->pix_fmt;
  1142. s->picture = *((AVFrame *)data);
  1143. s->picture.key_frame = 1;
  1144. s->picture.pict_type = FF_I_TYPE;
  1145. s->buf = buf;
  1146. c->execute(c, dv_encode_video_segment, s->sys->work_chunks, NULL,
  1147. dv_work_pool_size(s->sys), sizeof(DVwork_chunk));
  1148. emms_c();
  1149. dv_format_frame(s, buf);
  1150. return s->sys->frame_size;
  1151. }
  1152. #endif
  1153. static int dvvideo_close(AVCodecContext *c)
  1154. {
  1155. DVVideoContext *s = c->priv_data;
  1156. if (s->picture.data[0])
  1157. c->release_buffer(c, &s->picture);
  1158. return 0;
  1159. }
  1160. #if CONFIG_DVVIDEO_ENCODER
  1161. AVCodec dvvideo_encoder = {
  1162. "dvvideo",
  1163. CODEC_TYPE_VIDEO,
  1164. CODEC_ID_DVVIDEO,
  1165. sizeof(DVVideoContext),
  1166. dvvideo_init,
  1167. dvvideo_encode_frame,
  1168. .pix_fmts = (enum PixelFormat[]) {PIX_FMT_YUV411P, PIX_FMT_YUV422P, PIX_FMT_YUV420P, PIX_FMT_NONE},
  1169. .long_name = NULL_IF_CONFIG_SMALL("DV (Digital Video)"),
  1170. };
  1171. #endif // CONFIG_DVVIDEO_ENCODER
  1172. #if CONFIG_DVVIDEO_DECODER
  1173. AVCodec dvvideo_decoder = {
  1174. "dvvideo",
  1175. CODEC_TYPE_VIDEO,
  1176. CODEC_ID_DVVIDEO,
  1177. sizeof(DVVideoContext),
  1178. dvvideo_init,
  1179. NULL,
  1180. dvvideo_close,
  1181. dvvideo_decode_frame,
  1182. CODEC_CAP_DR1,
  1183. NULL,
  1184. .long_name = NULL_IF_CONFIG_SMALL("DV (Digital Video)"),
  1185. };
  1186. #endif