dv.c 43 KB

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