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

1222 lines
45KB

  1. /*
  2. * DV encoder
  3. * Copyright (c) 2003 Roman Shaposhnik
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * quant_deadzone code and fixes sponsored by NOA GmbH
  22. */
  23. /**
  24. * @file
  25. * DV encoder
  26. */
  27. #include "config.h"
  28. #include "libavutil/attributes.h"
  29. #include "libavutil/internal.h"
  30. #include "libavutil/mem_internal.h"
  31. #include "libavutil/opt.h"
  32. #include "libavutil/pixdesc.h"
  33. #include "avcodec.h"
  34. #include "dv.h"
  35. #include "dv_profile_internal.h"
  36. #include "dv_tablegen.h"
  37. #include "fdctdsp.h"
  38. #include "internal.h"
  39. #include "mathops.h"
  40. #include "me_cmp.h"
  41. #include "pixblockdsp.h"
  42. #include "put_bits.h"
  43. static av_cold int dvvideo_encode_init(AVCodecContext *avctx)
  44. {
  45. DVVideoContext *s = avctx->priv_data;
  46. FDCTDSPContext fdsp;
  47. MECmpContext mecc;
  48. PixblockDSPContext pdsp;
  49. int ret;
  50. s->sys = av_dv_codec_profile2(avctx->width, avctx->height, avctx->pix_fmt, avctx->time_base);
  51. if (!s->sys) {
  52. av_log(avctx, AV_LOG_ERROR, "Found no DV profile for %ix%i %s video. "
  53. "Valid DV profiles are:\n",
  54. avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
  55. ff_dv_print_profiles(avctx, AV_LOG_ERROR);
  56. return AVERROR(EINVAL);
  57. }
  58. ret = ff_dv_init_dynamic_tables(s, s->sys);
  59. if (ret < 0) {
  60. av_log(avctx, AV_LOG_ERROR, "Error initializing work tables.\n");
  61. return ret;
  62. }
  63. dv_vlc_map_tableinit();
  64. memset(&fdsp,0, sizeof(fdsp));
  65. memset(&mecc,0, sizeof(mecc));
  66. memset(&pdsp,0, sizeof(pdsp));
  67. ff_fdctdsp_init(&fdsp, avctx);
  68. ff_me_cmp_init(&mecc, avctx);
  69. ff_pixblockdsp_init(&pdsp, avctx);
  70. ff_set_cmp(&mecc, mecc.ildct_cmp, avctx->ildct_cmp);
  71. s->get_pixels = pdsp.get_pixels;
  72. s->ildct_cmp = mecc.ildct_cmp[5];
  73. s->fdct[0] = fdsp.fdct;
  74. s->fdct[1] = fdsp.fdct248;
  75. return ff_dvvideo_init(avctx);
  76. }
  77. /* bit budget for AC only in 5 MBs */
  78. static const int vs_total_ac_bits_hd = (68 * 6 + 52*2) * 5;
  79. static const int vs_total_ac_bits = (100 * 4 + 68 * 2) * 5;
  80. static const int mb_area_start[5] = { 1, 6, 21, 43, 64 };
  81. #if CONFIG_SMALL
  82. /* Convert run and level (where level != 0) pair into VLC, returning bit size */
  83. static av_always_inline int dv_rl2vlc(int run, int level, int sign,
  84. uint32_t *vlc)
  85. {
  86. int size;
  87. if (run < DV_VLC_MAP_RUN_SIZE && level < DV_VLC_MAP_LEV_SIZE) {
  88. *vlc = dv_vlc_map[run][level].vlc | sign;
  89. size = dv_vlc_map[run][level].size;
  90. } else {
  91. if (level < DV_VLC_MAP_LEV_SIZE) {
  92. *vlc = dv_vlc_map[0][level].vlc | sign;
  93. size = dv_vlc_map[0][level].size;
  94. } else {
  95. *vlc = 0xfe00 | (level << 1) | sign;
  96. size = 16;
  97. }
  98. if (run) {
  99. *vlc |= ((run < 16) ? dv_vlc_map[run - 1][0].vlc :
  100. (0x1f80 | (run - 1))) << size;
  101. size += (run < 16) ? dv_vlc_map[run - 1][0].size : 13;
  102. }
  103. }
  104. return size;
  105. }
  106. static av_always_inline int dv_rl2vlc_size(int run, int level)
  107. {
  108. int size;
  109. if (run < DV_VLC_MAP_RUN_SIZE && level < DV_VLC_MAP_LEV_SIZE) {
  110. size = dv_vlc_map[run][level].size;
  111. } else {
  112. size = (level < DV_VLC_MAP_LEV_SIZE) ? dv_vlc_map[0][level].size : 16;
  113. if (run)
  114. size += (run < 16) ? dv_vlc_map[run - 1][0].size : 13;
  115. }
  116. return size;
  117. }
  118. #else
  119. static av_always_inline int dv_rl2vlc(int run, int l, int sign, uint32_t *vlc)
  120. {
  121. *vlc = dv_vlc_map[run][l].vlc | sign;
  122. return dv_vlc_map[run][l].size;
  123. }
  124. static av_always_inline int dv_rl2vlc_size(int run, int l)
  125. {
  126. return dv_vlc_map[run][l].size;
  127. }
  128. #endif
  129. typedef struct EncBlockInfo {
  130. int area_q[4];
  131. int bit_size[4];
  132. int prev[5];
  133. int cur_ac;
  134. int cno;
  135. int dct_mode;
  136. int16_t mb[64];
  137. uint8_t next[64];
  138. uint8_t sign[64];
  139. uint8_t partial_bit_count;
  140. uint32_t partial_bit_buffer; /* we can't use uint16_t here */
  141. /* used by DV100 only: a copy of the weighted and classified but
  142. not-yet-quantized AC coefficients. This is necessary for
  143. re-quantizing at different steps. */
  144. int16_t save[64];
  145. int min_qlevel; /* DV100 only: minimum qlevel (for AC coefficients >255) */
  146. } EncBlockInfo;
  147. static av_always_inline PutBitContext *dv_encode_ac(EncBlockInfo *bi,
  148. PutBitContext *pb_pool,
  149. PutBitContext *pb_end)
  150. {
  151. int prev, bits_left;
  152. PutBitContext *pb = pb_pool;
  153. int size = bi->partial_bit_count;
  154. uint32_t vlc = bi->partial_bit_buffer;
  155. bi->partial_bit_count =
  156. bi->partial_bit_buffer = 0;
  157. for (;;) {
  158. /* Find suitable storage space */
  159. for (; size > (bits_left = put_bits_left(pb)); pb++) {
  160. if (bits_left) {
  161. size -= bits_left;
  162. put_bits(pb, bits_left, vlc >> size);
  163. vlc = av_mod_uintp2(vlc, size);
  164. }
  165. if (pb + 1 >= pb_end) {
  166. bi->partial_bit_count = size;
  167. bi->partial_bit_buffer = vlc;
  168. return pb;
  169. }
  170. }
  171. /* Store VLC */
  172. put_bits(pb, size, vlc);
  173. if (bi->cur_ac >= 64)
  174. break;
  175. /* Construct the next VLC */
  176. prev = bi->cur_ac;
  177. bi->cur_ac = bi->next[prev];
  178. if (bi->cur_ac < 64) {
  179. size = dv_rl2vlc(bi->cur_ac - prev - 1, bi->mb[bi->cur_ac],
  180. bi->sign[bi->cur_ac], &vlc);
  181. } else {
  182. size = 4;
  183. vlc = 6; /* End Of Block stamp */
  184. }
  185. }
  186. return pb;
  187. }
  188. static av_always_inline int dv_guess_dct_mode(DVVideoContext *s, uint8_t *data,
  189. ptrdiff_t linesize)
  190. {
  191. if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
  192. int ps = s->ildct_cmp(NULL, data, NULL, linesize, 8) - 400;
  193. if (ps > 0) {
  194. int is = s->ildct_cmp(NULL, data, NULL, linesize * 2, 4) +
  195. s->ildct_cmp(NULL, data + linesize, NULL, linesize * 2, 4);
  196. return ps > is;
  197. }
  198. }
  199. return 0;
  200. }
  201. static const int dv_weight_bits = 18;
  202. static const int dv_weight_88[64] = {
  203. 131072, 257107, 257107, 242189, 252167, 242189, 235923, 237536,
  204. 237536, 235923, 229376, 231390, 223754, 231390, 229376, 222935,
  205. 224969, 217965, 217965, 224969, 222935, 200636, 218652, 211916,
  206. 212325, 211916, 218652, 200636, 188995, 196781, 205965, 206433,
  207. 206433, 205965, 196781, 188995, 185364, 185364, 200636, 200704,
  208. 200636, 185364, 185364, 174609, 180568, 195068, 195068, 180568,
  209. 174609, 170091, 175557, 189591, 175557, 170091, 165371, 170627,
  210. 170627, 165371, 160727, 153560, 160727, 144651, 144651, 136258,
  211. };
  212. static const int dv_weight_248[64] = {
  213. 131072, 262144, 257107, 257107, 242189, 242189, 242189, 242189,
  214. 237536, 237536, 229376, 229376, 200636, 200636, 224973, 224973,
  215. 223754, 223754, 235923, 235923, 229376, 229376, 217965, 217965,
  216. 211916, 211916, 196781, 196781, 185364, 185364, 206433, 206433,
  217. 211916, 211916, 222935, 222935, 200636, 200636, 205964, 205964,
  218. 200704, 200704, 180568, 180568, 175557, 175557, 195068, 195068,
  219. 185364, 185364, 188995, 188995, 174606, 174606, 175557, 175557,
  220. 170627, 170627, 153560, 153560, 165371, 165371, 144651, 144651,
  221. };
  222. /* setting this to 1 results in a faster codec but
  223. * somewhat lower image quality */
  224. #define DV100_SACRIFICE_QUALITY_FOR_SPEED 1
  225. #define DV100_ENABLE_FINER 1
  226. /* pack combination of QNO and CNO into a single 8-bit value */
  227. #define DV100_MAKE_QLEVEL(qno,cno) ((qno<<2) | (cno))
  228. #define DV100_QLEVEL_QNO(qlevel) (qlevel>>2)
  229. #define DV100_QLEVEL_CNO(qlevel) (qlevel&0x3)
  230. #define DV100_NUM_QLEVELS 31
  231. /* The quantization step is determined by a combination of QNO and
  232. CNO. We refer to these combinations as "qlevels" (this term is our
  233. own, it's not mentioned in the spec). We use CNO, a multiplier on
  234. the quantization step, to "fill in the gaps" between quantization
  235. steps associated with successive values of QNO. e.g. there is no
  236. QNO for a quantization step of 10, but we can use QNO=5 CNO=1 to
  237. get the same result. The table below encodes combinations of QNO
  238. and CNO in order of increasing quantization coarseness. */
  239. static const uint8_t dv100_qlevels[DV100_NUM_QLEVELS] = {
  240. DV100_MAKE_QLEVEL( 1,0), // 1*1= 1
  241. DV100_MAKE_QLEVEL( 1,0), // 1*1= 1
  242. DV100_MAKE_QLEVEL( 2,0), // 2*1= 2
  243. DV100_MAKE_QLEVEL( 3,0), // 3*1= 3
  244. DV100_MAKE_QLEVEL( 4,0), // 4*1= 4
  245. DV100_MAKE_QLEVEL( 5,0), // 5*1= 5
  246. DV100_MAKE_QLEVEL( 6,0), // 6*1= 6
  247. DV100_MAKE_QLEVEL( 7,0), // 7*1= 7
  248. DV100_MAKE_QLEVEL( 8,0), // 8*1= 8
  249. DV100_MAKE_QLEVEL( 5,1), // 5*2=10
  250. DV100_MAKE_QLEVEL( 6,1), // 6*2=12
  251. DV100_MAKE_QLEVEL( 7,1), // 7*2=14
  252. DV100_MAKE_QLEVEL( 9,0), // 16*1=16
  253. DV100_MAKE_QLEVEL(10,0), // 18*1=18
  254. DV100_MAKE_QLEVEL(11,0), // 20*1=20
  255. DV100_MAKE_QLEVEL(12,0), // 22*1=22
  256. DV100_MAKE_QLEVEL(13,0), // 24*1=24
  257. DV100_MAKE_QLEVEL(14,0), // 28*1=28
  258. DV100_MAKE_QLEVEL( 9,1), // 16*2=32
  259. DV100_MAKE_QLEVEL(10,1), // 18*2=36
  260. DV100_MAKE_QLEVEL(11,1), // 20*2=40
  261. DV100_MAKE_QLEVEL(12,1), // 22*2=44
  262. DV100_MAKE_QLEVEL(13,1), // 24*2=48
  263. DV100_MAKE_QLEVEL(15,0), // 52*1=52
  264. DV100_MAKE_QLEVEL(14,1), // 28*2=56
  265. DV100_MAKE_QLEVEL( 9,2), // 16*4=64
  266. DV100_MAKE_QLEVEL(10,2), // 18*4=72
  267. DV100_MAKE_QLEVEL(11,2), // 20*4=80
  268. DV100_MAKE_QLEVEL(12,2), // 22*4=88
  269. DV100_MAKE_QLEVEL(13,2), // 24*4=96
  270. // ...
  271. DV100_MAKE_QLEVEL(15,3), // 52*8=416
  272. };
  273. static const int dv100_min_bias = 0;
  274. static const int dv100_chroma_bias = 0;
  275. static const int dv100_starting_qno = 1;
  276. #if DV100_SACRIFICE_QUALITY_FOR_SPEED
  277. static const int dv100_qlevel_inc = 4;
  278. #else
  279. static const int dv100_qlevel_inc = 1;
  280. #endif
  281. // 1/qstep, shifted up by 16 bits
  282. static const int dv100_qstep_bits = 16;
  283. static const int dv100_qstep_inv[16] = {
  284. 65536, 65536, 32768, 21845, 16384, 13107, 10923, 9362, 8192, 4096, 3641, 3277, 2979, 2731, 2341, 1260,
  285. };
  286. /* DV100 weights are pre-zigzagged, inverted and multiplied by 2^16
  287. (in DV100 the AC components are divided by the spec weights) */
  288. static const int dv_weight_1080[2][64] = {
  289. { 8192, 65536, 65536, 61681, 61681, 61681, 58254, 58254,
  290. 58254, 58254, 58254, 58254, 55188, 58254, 58254, 55188,
  291. 55188, 55188, 55188, 55188, 55188, 24966, 27594, 26214,
  292. 26214, 26214, 27594, 24966, 23831, 24385, 25575, 25575,
  293. 25575, 25575, 24385, 23831, 23302, 23302, 24966, 24966,
  294. 24966, 23302, 23302, 21845, 22795, 24385, 24385, 22795,
  295. 21845, 21400, 21845, 23831, 21845, 21400, 10382, 10700,
  296. 10700, 10382, 10082, 9620, 10082, 9039, 9039, 8525, },
  297. { 8192, 65536, 65536, 61681, 61681, 61681, 41943, 41943,
  298. 41943, 41943, 40330, 41943, 40330, 41943, 40330, 40330,
  299. 40330, 38836, 38836, 40330, 40330, 24966, 27594, 26214,
  300. 26214, 26214, 27594, 24966, 23831, 24385, 25575, 25575,
  301. 25575, 25575, 24385, 23831, 11523, 11523, 12483, 12483,
  302. 12483, 11523, 11523, 10923, 11275, 12193, 12193, 11275,
  303. 10923, 5323, 5490, 5924, 5490, 5323, 5165, 5323,
  304. 5323, 5165, 5017, 4788, 5017, 4520, 4520, 4263, }
  305. };
  306. static const int dv_weight_720[2][64] = {
  307. { 8192, 65536, 65536, 61681, 61681, 61681, 58254, 58254,
  308. 58254, 58254, 58254, 58254, 55188, 58254, 58254, 55188,
  309. 55188, 55188, 55188, 55188, 55188, 24966, 27594, 26214,
  310. 26214, 26214, 27594, 24966, 23831, 24385, 25575, 25575,
  311. 25575, 25575, 24385, 23831, 15420, 15420, 16644, 16644,
  312. 16644, 15420, 15420, 10923, 11398, 12193, 12193, 11398,
  313. 10923, 10700, 10923, 11916, 10923, 10700, 5191, 5350,
  314. 5350, 5191, 5041, 4810, 5041, 4520, 4520, 4263, },
  315. { 8192, 43691, 43691, 40330, 40330, 40330, 29127, 29127,
  316. 29127, 29127, 29127, 29127, 27594, 29127, 29127, 27594,
  317. 27594, 27594, 27594, 27594, 27594, 12483, 13797, 13107,
  318. 13107, 13107, 13797, 12483, 11916, 12193, 12788, 12788,
  319. 12788, 12788, 12193, 11916, 5761, 5761, 6242, 6242,
  320. 6242, 5761, 5761, 5461, 5638, 5461, 6096, 5638,
  321. 5461, 2661, 2745, 2962, 2745, 2661, 2583, 2661,
  322. 2661, 2583, 2509, 2394, 2509, 2260, 2260, 2131, }
  323. };
  324. static av_always_inline int dv_set_class_number_sd(DVVideoContext *s,
  325. int16_t *blk, EncBlockInfo *bi,
  326. const uint8_t *zigzag_scan,
  327. const int *weight, int bias)
  328. {
  329. int i, area;
  330. /* We offer two different methods for class number assignment: the
  331. * method suggested in SMPTE 314M Table 22, and an improved
  332. * method. The SMPTE method is very conservative; it assigns class
  333. * 3 (i.e. severe quantization) to any block where the largest AC
  334. * component is greater than 36. FFmpeg's DV encoder tracks AC bit
  335. * consumption precisely, so there is no need to bias most blocks
  336. * towards strongly lossy compression. Instead, we assign class 2
  337. * to most blocks, and use class 3 only when strictly necessary
  338. * (for blocks whose largest AC component exceeds 255). */
  339. #if 0 /* SMPTE spec method */
  340. static const int classes[] = { 12, 24, 36, 0xffff };
  341. #else /* improved FFmpeg method */
  342. static const int classes[] = { -1, -1, 255, 0xffff };
  343. #endif
  344. int max = classes[0];
  345. int prev = 0;
  346. const unsigned deadzone = s->quant_deadzone;
  347. const unsigned threshold = 2 * deadzone;
  348. bi->mb[0] = blk[0];
  349. for (area = 0; area < 4; area++) {
  350. bi->prev[area] = prev;
  351. bi->bit_size[area] = 1; // 4 areas 4 bits for EOB :)
  352. for (i = mb_area_start[area]; i < mb_area_start[area + 1]; i++) {
  353. int level = blk[zigzag_scan[i]];
  354. if (level + deadzone > threshold) {
  355. bi->sign[i] = (level >> 31) & 1;
  356. /* Weight it and shift down into range, adding for rounding.
  357. * The extra division by a factor of 2^4 reverses the 8x
  358. * expansion of the DCT AND the 2x doubling of the weights. */
  359. level = (FFABS(level) * weight[i] + (1 << (dv_weight_bits + 3))) >>
  360. (dv_weight_bits + 4);
  361. if (!level)
  362. continue;
  363. bi->mb[i] = level;
  364. if (level > max)
  365. max = level;
  366. bi->bit_size[area] += dv_rl2vlc_size(i - prev - 1, level);
  367. bi->next[prev] = i;
  368. prev = i;
  369. }
  370. }
  371. }
  372. bi->next[prev] = i;
  373. for (bi->cno = 0; max > classes[bi->cno]; bi->cno++)
  374. ;
  375. bi->cno += bias;
  376. if (bi->cno >= 3) {
  377. bi->cno = 3;
  378. prev = 0;
  379. i = bi->next[prev];
  380. for (area = 0; area < 4; area++) {
  381. bi->prev[area] = prev;
  382. bi->bit_size[area] = 1; // 4 areas 4 bits for EOB :)
  383. for (; i < mb_area_start[area + 1]; i = bi->next[i]) {
  384. bi->mb[i] >>= 1;
  385. if (bi->mb[i]) {
  386. bi->bit_size[area] += dv_rl2vlc_size(i - prev - 1, bi->mb[i]);
  387. bi->next[prev] = i;
  388. prev = i;
  389. }
  390. }
  391. }
  392. bi->next[prev] = i;
  393. }
  394. return bi->bit_size[0] + bi->bit_size[1] +
  395. bi->bit_size[2] + bi->bit_size[3];
  396. }
  397. /* this function just copies the DCT coefficients and performs
  398. the initial (non-)quantization. */
  399. static inline void dv_set_class_number_hd(DVVideoContext *s,
  400. int16_t *blk, EncBlockInfo *bi,
  401. const uint8_t *zigzag_scan,
  402. const int *weight, int bias)
  403. {
  404. int i, max = 0;
  405. /* the first quantization (none at all) */
  406. bi->area_q[0] = 1;
  407. /* weigh AC components and store to save[] */
  408. /* (i=0 is the DC component; we only include it to make the
  409. number of loop iterations even, for future possible SIMD optimization) */
  410. for (i = 0; i < 64; i += 2) {
  411. int level0, level1;
  412. /* get the AC component (in zig-zag order) */
  413. level0 = blk[zigzag_scan[i+0]];
  414. level1 = blk[zigzag_scan[i+1]];
  415. /* extract sign and make it the lowest bit */
  416. bi->sign[i+0] = (level0>>31)&1;
  417. bi->sign[i+1] = (level1>>31)&1;
  418. /* take absolute value of the level */
  419. level0 = FFABS(level0);
  420. level1 = FFABS(level1);
  421. /* weigh it */
  422. level0 = (level0*weight[i+0] + 4096 + (1<<17)) >> 18;
  423. level1 = (level1*weight[i+1] + 4096 + (1<<17)) >> 18;
  424. /* save unquantized value */
  425. bi->save[i+0] = level0;
  426. bi->save[i+1] = level1;
  427. /* find max component */
  428. if (bi->save[i+0] > max)
  429. max = bi->save[i+0];
  430. if (bi->save[i+1] > max)
  431. max = bi->save[i+1];
  432. }
  433. /* copy DC component */
  434. bi->mb[0] = blk[0];
  435. /* the EOB code is 4 bits */
  436. bi->bit_size[0] = 4;
  437. bi->bit_size[1] = bi->bit_size[2] = bi->bit_size[3] = 0;
  438. /* ensure that no AC coefficients are cut off */
  439. bi->min_qlevel = ((max+256) >> 8);
  440. bi->area_q[0] = 25; /* set to an "impossible" value */
  441. bi->cno = 0;
  442. }
  443. static av_always_inline int dv_init_enc_block(EncBlockInfo* bi, uint8_t *data, int linesize,
  444. DVVideoContext *s, int chroma)
  445. {
  446. LOCAL_ALIGNED_16(int16_t, blk, [64]);
  447. bi->area_q[0] = bi->area_q[1] = bi->area_q[2] = bi->area_q[3] = 0;
  448. bi->partial_bit_count = 0;
  449. bi->partial_bit_buffer = 0;
  450. bi->cur_ac = 0;
  451. if (data) {
  452. if (DV_PROFILE_IS_HD(s->sys)) {
  453. s->get_pixels(blk, data, linesize * (1 << bi->dct_mode));
  454. s->fdct[0](blk);
  455. } else {
  456. bi->dct_mode = dv_guess_dct_mode(s, data, linesize);
  457. s->get_pixels(blk, data, linesize);
  458. s->fdct[bi->dct_mode](blk);
  459. }
  460. } else {
  461. /* We rely on the fact that encoding all zeros leads to an immediate EOB,
  462. which is precisely what the spec calls for in the "dummy" blocks. */
  463. memset(blk, 0, 64*sizeof(*blk));
  464. bi->dct_mode = 0;
  465. }
  466. if (DV_PROFILE_IS_HD(s->sys)) {
  467. const int *weights;
  468. if (s->sys->height == 1080) {
  469. weights = dv_weight_1080[chroma];
  470. } else { /* 720p */
  471. weights = dv_weight_720[chroma];
  472. }
  473. dv_set_class_number_hd(s, blk, bi,
  474. ff_zigzag_direct,
  475. weights,
  476. dv100_min_bias+chroma*dv100_chroma_bias);
  477. } else {
  478. dv_set_class_number_sd(s, blk, bi,
  479. bi->dct_mode ? ff_dv_zigzag248_direct : ff_zigzag_direct,
  480. bi->dct_mode ? dv_weight_248 : dv_weight_88,
  481. chroma);
  482. }
  483. return bi->bit_size[0] + bi->bit_size[1] + bi->bit_size[2] + bi->bit_size[3];
  484. }
  485. /* DV100 quantize
  486. Perform quantization by divinding the AC component by the qstep.
  487. As an optimization we use a fixed-point integer multiply instead
  488. of a divide. */
  489. static av_always_inline int dv100_quantize(int level, int qsinv)
  490. {
  491. /* this code is equivalent to */
  492. /* return (level + qs/2) / qs; */
  493. return (level * qsinv + 1024 + (1<<(dv100_qstep_bits-1))) >> dv100_qstep_bits;
  494. /* the extra +1024 is needed to make the rounding come out right. */
  495. /* I (DJM) have verified that the results are exactly the same as
  496. division for level 0-2048 at all QNOs. */
  497. }
  498. static int dv100_actual_quantize(EncBlockInfo *b, int qlevel)
  499. {
  500. int prev, k, qsinv;
  501. int qno = DV100_QLEVEL_QNO(dv100_qlevels[qlevel]);
  502. int cno = DV100_QLEVEL_CNO(dv100_qlevels[qlevel]);
  503. if (b->area_q[0] == qno && b->cno == cno)
  504. return b->bit_size[0];
  505. qsinv = dv100_qstep_inv[qno];
  506. /* record the new qstep */
  507. b->area_q[0] = qno;
  508. b->cno = cno;
  509. /* reset encoded size (EOB = 4 bits) */
  510. b->bit_size[0] = 4;
  511. /* visit nonzero components and quantize */
  512. prev = 0;
  513. for (k = 1; k < 64; k++) {
  514. /* quantize */
  515. int ac = dv100_quantize(b->save[k], qsinv) >> cno;
  516. if (ac) {
  517. if (ac > 255)
  518. ac = 255;
  519. b->mb[k] = ac;
  520. b->bit_size[0] += dv_rl2vlc_size(k - prev - 1, ac);
  521. b->next[prev] = k;
  522. prev = k;
  523. }
  524. }
  525. b->next[prev] = k;
  526. return b->bit_size[0];
  527. }
  528. static inline void dv_guess_qnos_hd(EncBlockInfo *blks, int *qnos)
  529. {
  530. EncBlockInfo *b;
  531. int min_qlevel[5];
  532. int qlevels[5];
  533. int size[5];
  534. int i, j;
  535. /* cache block sizes at hypothetical qlevels */
  536. uint16_t size_cache[5*8][DV100_NUM_QLEVELS] = {{0}};
  537. /* get minimum qlevels */
  538. for (i = 0; i < 5; i++) {
  539. min_qlevel[i] = 1;
  540. for (j = 0; j < 8; j++) {
  541. if (blks[8*i+j].min_qlevel > min_qlevel[i])
  542. min_qlevel[i] = blks[8*i+j].min_qlevel;
  543. }
  544. }
  545. /* initialize sizes */
  546. for (i = 0; i < 5; i++) {
  547. qlevels[i] = dv100_starting_qno;
  548. if (qlevels[i] < min_qlevel[i])
  549. qlevels[i] = min_qlevel[i];
  550. qnos[i] = DV100_QLEVEL_QNO(dv100_qlevels[qlevels[i]]);
  551. size[i] = 0;
  552. for (j = 0; j < 8; j++) {
  553. size_cache[8*i+j][qlevels[i]] = dv100_actual_quantize(&blks[8*i+j], qlevels[i]);
  554. size[i] += size_cache[8*i+j][qlevels[i]];
  555. }
  556. }
  557. /* must we go coarser? */
  558. if (size[0]+size[1]+size[2]+size[3]+size[4] > vs_total_ac_bits_hd) {
  559. int largest = size[0] % 5; /* 'random' number */
  560. int qlevels_done = 0;
  561. do {
  562. /* find the macroblock with the lowest qlevel */
  563. for (i = 0; i < 5; i++) {
  564. if (qlevels[i] < qlevels[largest])
  565. largest = i;
  566. }
  567. i = largest;
  568. /* ensure that we don't enter infinite loop */
  569. largest = (largest+1) % 5;
  570. /* quantize a little bit more */
  571. qlevels[i] += dv100_qlevel_inc;
  572. if (qlevels[i] > DV100_NUM_QLEVELS-1) {
  573. qlevels[i] = DV100_NUM_QLEVELS-1;
  574. qlevels_done++;
  575. }
  576. qnos[i] = DV100_QLEVEL_QNO(dv100_qlevels[qlevels[i]]);
  577. size[i] = 0;
  578. /* for each block */
  579. b = &blks[8*i];
  580. for (j = 0; j < 8; j++, b++) {
  581. /* accumulate block size into macroblock */
  582. if(size_cache[8*i+j][qlevels[i]] == 0) {
  583. /* it is safe to use actual_quantize() here because we only go from finer to coarser,
  584. and it saves the final actual_quantize() down below */
  585. size_cache[8*i+j][qlevels[i]] = dv100_actual_quantize(b, qlevels[i]);
  586. }
  587. size[i] += size_cache[8*i+j][qlevels[i]];
  588. } /* for each block */
  589. } while (vs_total_ac_bits_hd < size[0] + size[1] + size[2] + size[3] + size[4] && qlevels_done < 5);
  590. // can we go finer?
  591. } else if (DV100_ENABLE_FINER &&
  592. size[0]+size[1]+size[2]+size[3]+size[4] < vs_total_ac_bits_hd) {
  593. int save_qlevel;
  594. int largest = size[0] % 5; /* 'random' number */
  595. while (qlevels[0] > min_qlevel[0] ||
  596. qlevels[1] > min_qlevel[1] ||
  597. qlevels[2] > min_qlevel[2] ||
  598. qlevels[3] > min_qlevel[3] ||
  599. qlevels[4] > min_qlevel[4]) {
  600. /* find the macroblock with the highest qlevel */
  601. for (i = 0; i < 5; i++) {
  602. if (qlevels[i] > min_qlevel[i] && qlevels[i] > qlevels[largest])
  603. largest = i;
  604. }
  605. i = largest;
  606. /* ensure that we don't enter infinite loop */
  607. largest = (largest+1) % 5;
  608. if (qlevels[i] <= min_qlevel[i]) {
  609. /* can't unquantize any more */
  610. continue;
  611. }
  612. /* quantize a little bit less */
  613. save_qlevel = qlevels[i];
  614. qlevels[i] -= dv100_qlevel_inc;
  615. if (qlevels[i] < min_qlevel[i])
  616. qlevels[i] = min_qlevel[i];
  617. qnos[i] = DV100_QLEVEL_QNO(dv100_qlevels[qlevels[i]]);
  618. size[i] = 0;
  619. /* for each block */
  620. b = &blks[8*i];
  621. for (j = 0; j < 8; j++, b++) {
  622. /* accumulate block size into macroblock */
  623. if(size_cache[8*i+j][qlevels[i]] == 0) {
  624. size_cache[8*i+j][qlevels[i]] = dv100_actual_quantize(b, qlevels[i]);
  625. }
  626. size[i] += size_cache[8*i+j][qlevels[i]];
  627. } /* for each block */
  628. /* did we bust the limit? */
  629. if (vs_total_ac_bits_hd < size[0] + size[1] + size[2] + size[3] + size[4]) {
  630. /* go back down and exit */
  631. qlevels[i] = save_qlevel;
  632. qnos[i] = DV100_QLEVEL_QNO(dv100_qlevels[qlevels[i]]);
  633. break;
  634. }
  635. }
  636. }
  637. /* now do the actual quantization */
  638. for (i = 0; i < 5; i++) {
  639. /* for each block */
  640. b = &blks[8*i];
  641. size[i] = 0;
  642. for (j = 0; j < 8; j++, b++) {
  643. /* accumulate block size into macroblock */
  644. size[i] += dv100_actual_quantize(b, qlevels[i]);
  645. } /* for each block */
  646. }
  647. }
  648. static inline void dv_guess_qnos(EncBlockInfo *blks, int *qnos)
  649. {
  650. int size[5];
  651. int i, j, k, a, prev, a2;
  652. EncBlockInfo *b;
  653. size[0] =
  654. size[1] =
  655. size[2] =
  656. size[3] =
  657. size[4] = 1 << 24;
  658. do {
  659. b = blks;
  660. for (i = 0; i < 5; i++) {
  661. if (!qnos[i])
  662. continue;
  663. qnos[i]--;
  664. size[i] = 0;
  665. for (j = 0; j < 6; j++, b++) {
  666. for (a = 0; a < 4; a++) {
  667. if (b->area_q[a] != ff_dv_quant_shifts[qnos[i] + ff_dv_quant_offset[b->cno]][a]) {
  668. b->bit_size[a] = 1; // 4 areas 4 bits for EOB :)
  669. b->area_q[a]++;
  670. prev = b->prev[a];
  671. av_assert2(b->next[prev] >= mb_area_start[a + 1] || b->mb[prev]);
  672. for (k = b->next[prev]; k < mb_area_start[a + 1]; k = b->next[k]) {
  673. b->mb[k] >>= 1;
  674. if (b->mb[k]) {
  675. b->bit_size[a] += dv_rl2vlc_size(k - prev - 1, b->mb[k]);
  676. prev = k;
  677. } else {
  678. if (b->next[k] >= mb_area_start[a + 1] && b->next[k] < 64) {
  679. for (a2 = a + 1; b->next[k] >= mb_area_start[a2 + 1]; a2++)
  680. b->prev[a2] = prev;
  681. av_assert2(a2 < 4);
  682. av_assert2(b->mb[b->next[k]]);
  683. b->bit_size[a2] += dv_rl2vlc_size(b->next[k] - prev - 1, b->mb[b->next[k]]) -
  684. dv_rl2vlc_size(b->next[k] - k - 1, b->mb[b->next[k]]);
  685. av_assert2(b->prev[a2] == k && (a2 + 1 >= 4 || b->prev[a2 + 1] != k));
  686. b->prev[a2] = prev;
  687. }
  688. b->next[prev] = b->next[k];
  689. }
  690. }
  691. b->prev[a + 1] = prev;
  692. }
  693. size[i] += b->bit_size[a];
  694. }
  695. }
  696. if (vs_total_ac_bits >= size[0] + size[1] + size[2] + size[3] + size[4])
  697. return;
  698. }
  699. } while (qnos[0] | qnos[1] | qnos[2] | qnos[3] | qnos[4]);
  700. for (a = 2; a == 2 || vs_total_ac_bits < size[0]; a += a) {
  701. b = blks;
  702. size[0] = 5 * 6 * 4; // EOB
  703. for (j = 0; j < 6 * 5; j++, b++) {
  704. prev = b->prev[0];
  705. for (k = b->next[prev]; k < 64; k = b->next[k]) {
  706. if (b->mb[k] < a && b->mb[k] > -a) {
  707. b->next[prev] = b->next[k];
  708. } else {
  709. size[0] += dv_rl2vlc_size(k - prev - 1, b->mb[k]);
  710. prev = k;
  711. }
  712. }
  713. }
  714. }
  715. }
  716. /* update all cno values into the blocks, over-writing the old values without
  717. touching anything else. (only used for DV100) */
  718. static inline void dv_revise_cnos(uint8_t *dif, EncBlockInfo *blk, const AVDVProfile *profile)
  719. {
  720. uint8_t *data;
  721. int mb_index, i;
  722. for (mb_index = 0; mb_index < 5; mb_index++) {
  723. data = dif + mb_index*80 + 4;
  724. for (i = 0; i < profile->bpm; i++) {
  725. /* zero out the class number */
  726. data[1] &= 0xCF;
  727. /* add the new one */
  728. data[1] |= blk[profile->bpm*mb_index+i].cno << 4;
  729. data += profile->block_sizes[i] >> 3;
  730. }
  731. }
  732. }
  733. static int dv_encode_video_segment(AVCodecContext *avctx, void *arg)
  734. {
  735. DVVideoContext *s = avctx->priv_data;
  736. DVwork_chunk *work_chunk = arg;
  737. int mb_index, i, j;
  738. int mb_x, mb_y, c_offset;
  739. ptrdiff_t linesize, y_stride;
  740. uint8_t *y_ptr;
  741. uint8_t *dif, *p;
  742. LOCAL_ALIGNED_8(uint8_t, scratch, [128]);
  743. EncBlockInfo enc_blks[5 * DV_MAX_BPM];
  744. PutBitContext pbs[5 * DV_MAX_BPM];
  745. PutBitContext *pb;
  746. EncBlockInfo *enc_blk;
  747. int vs_bit_size = 0;
  748. int qnos[5];
  749. int *qnosp = &qnos[0];
  750. p = dif = &s->buf[work_chunk->buf_offset * 80];
  751. enc_blk = &enc_blks[0];
  752. for (mb_index = 0; mb_index < 5; mb_index++) {
  753. dv_calculate_mb_xy(s, work_chunk, mb_index, &mb_x, &mb_y);
  754. qnos[mb_index] = DV_PROFILE_IS_HD(s->sys) ? 1 : 15;
  755. y_ptr = s->frame->data[0] + (mb_y * s->frame->linesize[0] + mb_x) * 8;
  756. linesize = s->frame->linesize[0];
  757. if (s->sys->height == 1080 && mb_y < 134)
  758. enc_blk->dct_mode = dv_guess_dct_mode(s, y_ptr, linesize);
  759. else
  760. enc_blk->dct_mode = 0;
  761. for (i = 1; i < 8; i++)
  762. enc_blk[i].dct_mode = enc_blk->dct_mode;
  763. /* initializing luminance blocks */
  764. if ((s->sys->pix_fmt == AV_PIX_FMT_YUV420P) ||
  765. (s->sys->pix_fmt == AV_PIX_FMT_YUV411P && mb_x >= (704 / 8)) ||
  766. (s->sys->height >= 720 && mb_y != 134)) {
  767. y_stride = s->frame->linesize[0] * (1 << (3*!enc_blk->dct_mode));
  768. } else {
  769. y_stride = 16;
  770. }
  771. y_ptr = s->frame->data[0] +
  772. (mb_y * s->frame->linesize[0] + mb_x) * 8;
  773. linesize = s->frame->linesize[0];
  774. if (s->sys->video_stype == 4) { /* SD 422 */
  775. vs_bit_size +=
  776. dv_init_enc_block(enc_blk + 0, y_ptr, linesize, s, 0) +
  777. dv_init_enc_block(enc_blk + 1, NULL, linesize, s, 0) +
  778. dv_init_enc_block(enc_blk + 2, y_ptr + 8, linesize, s, 0) +
  779. dv_init_enc_block(enc_blk + 3, NULL, linesize, s, 0);
  780. } else {
  781. vs_bit_size +=
  782. dv_init_enc_block(enc_blk + 0, y_ptr, linesize, s, 0) +
  783. dv_init_enc_block(enc_blk + 1, y_ptr + 8, linesize, s, 0) +
  784. dv_init_enc_block(enc_blk + 2, y_ptr + y_stride, linesize, s, 0) +
  785. dv_init_enc_block(enc_blk + 3, y_ptr + 8 + y_stride, linesize, s, 0);
  786. }
  787. enc_blk += 4;
  788. /* initializing chrominance blocks */
  789. c_offset = ((mb_y >> (s->sys->pix_fmt == AV_PIX_FMT_YUV420P)) * s->frame->linesize[1] +
  790. (mb_x >> ((s->sys->pix_fmt == AV_PIX_FMT_YUV411P) ? 2 : 1))) * 8;
  791. for (j = 2; j; j--) {
  792. uint8_t *c_ptr = s->frame->data[j] + c_offset;
  793. linesize = s->frame->linesize[j];
  794. y_stride = (mb_y == 134) ? 8 : (s->frame->linesize[j] * (1 << (3*!enc_blk->dct_mode)));
  795. if (s->sys->pix_fmt == AV_PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
  796. uint8_t *d;
  797. uint8_t *b = scratch;
  798. for (i = 0; i < 8; i++) {
  799. d = c_ptr + linesize * 8;
  800. b[0] = c_ptr[0];
  801. b[1] = c_ptr[1];
  802. b[2] = c_ptr[2];
  803. b[3] = c_ptr[3];
  804. b[4] = d[0];
  805. b[5] = d[1];
  806. b[6] = d[2];
  807. b[7] = d[3];
  808. c_ptr += linesize;
  809. b += 16;
  810. }
  811. c_ptr = scratch;
  812. linesize = 16;
  813. }
  814. vs_bit_size += dv_init_enc_block(enc_blk++, c_ptr, linesize, s, 1);
  815. if (s->sys->bpm == 8)
  816. vs_bit_size += dv_init_enc_block(enc_blk++, c_ptr + y_stride,
  817. linesize, s, 1);
  818. }
  819. }
  820. if (DV_PROFILE_IS_HD(s->sys)) {
  821. /* unconditional */
  822. dv_guess_qnos_hd(&enc_blks[0], qnosp);
  823. } else if (vs_total_ac_bits < vs_bit_size) {
  824. dv_guess_qnos(&enc_blks[0], qnosp);
  825. }
  826. /* DIF encoding process */
  827. for (j = 0; j < 5 * s->sys->bpm;) {
  828. int start_mb = j;
  829. p[3] = *qnosp++;
  830. p += 4;
  831. /* First pass over individual cells only */
  832. for (i = 0; i < s->sys->bpm; i++, j++) {
  833. int sz = s->sys->block_sizes[i] >> 3;
  834. init_put_bits(&pbs[j], p, sz);
  835. put_sbits(&pbs[j], 9, ((enc_blks[j].mb[0] >> 3) - 1024 + 2) >> 2);
  836. put_bits(&pbs[j], 1, DV_PROFILE_IS_HD(s->sys) && i ? 1 : enc_blks[j].dct_mode);
  837. put_bits(&pbs[j], 2, enc_blks[j].cno);
  838. dv_encode_ac(&enc_blks[j], &pbs[j], &pbs[j + 1]);
  839. p += sz;
  840. }
  841. /* Second pass over each MB space */
  842. pb = &pbs[start_mb];
  843. for (i = 0; i < s->sys->bpm; i++)
  844. if (enc_blks[start_mb + i].partial_bit_count)
  845. pb = dv_encode_ac(&enc_blks[start_mb + i], pb,
  846. &pbs[start_mb + s->sys->bpm]);
  847. }
  848. /* Third and final pass over the whole video segment space */
  849. pb = &pbs[0];
  850. for (j = 0; j < 5 * s->sys->bpm; j++) {
  851. if (enc_blks[j].partial_bit_count)
  852. pb = dv_encode_ac(&enc_blks[j], pb, &pbs[s->sys->bpm * 5]);
  853. if (enc_blks[j].partial_bit_count)
  854. av_log(avctx, AV_LOG_ERROR, "ac bitstream overflow\n");
  855. }
  856. for (j = 0; j < 5 * s->sys->bpm; j++) {
  857. flush_put_bits(&pbs[j]);
  858. memset(put_bits_ptr(&pbs[j]), 0xff, put_bytes_left(&pbs[j], 0));
  859. }
  860. if (DV_PROFILE_IS_HD(s->sys))
  861. dv_revise_cnos(dif, enc_blks, s->sys);
  862. return 0;
  863. }
  864. static inline int dv_write_pack(enum dv_pack_type pack_id, DVVideoContext *c,
  865. uint8_t *buf)
  866. {
  867. /*
  868. * Here's what SMPTE314M says about these two:
  869. * (page 6) APTn, AP1n, AP2n, AP3n: These data shall be identical
  870. * as track application IDs (APTn = 001, AP1n =
  871. * 001, AP2n = 001, AP3n = 001), if the source signal
  872. * comes from a digital VCR. If the signal source is
  873. * unknown, all bits for these data shall be set to 1.
  874. * (page 12) STYPE: STYPE defines a signal type of video signal
  875. * 00000b = 4:1:1 compression
  876. * 00100b = 4:2:2 compression
  877. * XXXXXX = Reserved
  878. * Now, I've got two problems with these statements:
  879. * 1. it looks like APT == 111b should be a safe bet, but it isn't.
  880. * It seems that for PAL as defined in IEC 61834 we have to set
  881. * APT to 000 and for SMPTE314M to 001.
  882. * 2. It is not at all clear what STYPE is used for 4:2:0 PAL
  883. * compression scheme (if any).
  884. */
  885. uint8_t aspect = 0;
  886. int apt = (c->sys->pix_fmt == AV_PIX_FMT_YUV420P ? 0 : 1);
  887. int fs;
  888. if (c->avctx->height >= 720)
  889. fs = c->avctx->height == 720 || c->frame->top_field_first ? 0x40 : 0x00;
  890. else
  891. fs = c->frame->top_field_first ? 0x00 : 0x40;
  892. if (DV_PROFILE_IS_HD(c->sys) ||
  893. (int)(av_q2d(c->avctx->sample_aspect_ratio) *
  894. c->avctx->width / c->avctx->height * 10) >= 17)
  895. /* HD formats are always 16:9 */
  896. aspect = 0x02;
  897. buf[0] = (uint8_t) pack_id;
  898. switch (pack_id) {
  899. case dv_header525: /* I can't imagine why these two weren't defined as real */
  900. case dv_header625: /* packs in SMPTE314M -- they definitely look like ones */
  901. buf[1] = 0xf8 | /* reserved -- always 1 */
  902. (apt & 0x07); /* APT: Track application ID */
  903. buf[2] = (0 << 7) | /* TF1: audio data is 0 - valid; 1 - invalid */
  904. (0x0f << 3) | /* reserved -- always 1 */
  905. (apt & 0x07); /* AP1: Audio application ID */
  906. buf[3] = (0 << 7) | /* TF2: video data is 0 - valid; 1 - invalid */
  907. (0x0f << 3) | /* reserved -- always 1 */
  908. (apt & 0x07); /* AP2: Video application ID */
  909. buf[4] = (0 << 7) | /* TF3: subcode(SSYB) is 0 - valid; 1 - invalid */
  910. (0x0f << 3) | /* reserved -- always 1 */
  911. (apt & 0x07); /* AP3: Subcode application ID */
  912. break;
  913. case dv_video_source:
  914. buf[1] = 0xff; /* reserved -- always 1 */
  915. buf[2] = (1 << 7) | /* B/W: 0 - b/w, 1 - color */
  916. (1 << 6) | /* following CLF is valid - 0, invalid - 1 */
  917. (3 << 4) | /* CLF: color frames ID (see ITU-R BT.470-4) */
  918. 0xf; /* reserved -- always 1 */
  919. buf[3] = (3 << 6) | /* reserved -- always 1 */
  920. (c->sys->dsf << 5) | /* system: 60fields/50fields */
  921. c->sys->video_stype; /* signal type video compression */
  922. buf[4] = 0xff; /* VISC: 0xff -- no information */
  923. break;
  924. case dv_video_control:
  925. buf[1] = (0 << 6) | /* Copy generation management (CGMS) 0 -- free */
  926. 0x3f; /* reserved -- always 1 */
  927. buf[2] = 0xc8 | /* reserved -- always b11001xxx */
  928. aspect;
  929. buf[3] = (1 << 7) | /* frame/field flag 1 -- frame, 0 -- field */
  930. fs | /* first/second field flag 0 -- field 2, 1 -- field 1 */
  931. (1 << 5) | /* frame change flag 0 -- same picture as before, 1 -- different */
  932. (1 << 4) | /* 1 - interlaced, 0 - noninterlaced */
  933. 0xc; /* reserved -- always b1100 */
  934. buf[4] = 0xff; /* reserved -- always 1 */
  935. break;
  936. default:
  937. buf[1] =
  938. buf[2] =
  939. buf[3] =
  940. buf[4] = 0xff;
  941. }
  942. return 5;
  943. }
  944. static inline int dv_write_dif_id(enum dv_section_type t, uint8_t chan_num,
  945. uint8_t seq_num, uint8_t dif_num,
  946. uint8_t *buf)
  947. {
  948. int fsc = chan_num & 1;
  949. int fsp = 1 - (chan_num >> 1);
  950. buf[0] = (uint8_t) t; /* Section type */
  951. buf[1] = (seq_num << 4) | /* DIF seq number 0-9 for 525/60; 0-11 for 625/50 */
  952. (fsc << 3) | /* FSC: for 50 and 100Mb/s 0 - first channel; 1 - second */
  953. (fsp << 2) | /* FSP: for 100Mb/s 1 - channels 0-1; 0 - channels 2-3 */
  954. 3; /* reserved -- always 1 */
  955. buf[2] = dif_num; /* DIF block number Video: 0-134, Audio: 0-8 */
  956. return 3;
  957. }
  958. static inline int dv_write_ssyb_id(uint8_t syb_num, uint8_t fr, uint8_t *buf)
  959. {
  960. if (syb_num == 0 || syb_num == 6) {
  961. buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */
  962. (0 << 4) | /* AP3 (Subcode application ID) */
  963. 0x0f; /* reserved -- always 1 */
  964. } else if (syb_num == 11) {
  965. buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */
  966. 0x7f; /* reserved -- always 1 */
  967. } else {
  968. buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */
  969. (0 << 4) | /* APT (Track application ID) */
  970. 0x0f; /* reserved -- always 1 */
  971. }
  972. buf[1] = 0xf0 | /* reserved -- always 1 */
  973. (syb_num & 0x0f); /* SSYB number 0 - 11 */
  974. buf[2] = 0xff; /* reserved -- always 1 */
  975. return 3;
  976. }
  977. static void dv_format_frame(DVVideoContext *c, uint8_t *buf)
  978. {
  979. int chan, i, j, k;
  980. /* We work with 720p frames split in half. The odd half-frame is chan 2,3 */
  981. int chan_offset = 2*(c->sys->height == 720 && c->avctx->frame_number & 1);
  982. for (chan = 0; chan < c->sys->n_difchan; chan++) {
  983. for (i = 0; i < c->sys->difseg_size; i++) {
  984. memset(buf, 0xff, 80 * 6); /* first 6 DIF blocks are for control data */
  985. /* DV header: 1DIF */
  986. buf += dv_write_dif_id(dv_sect_header, chan+chan_offset, i, 0, buf);
  987. buf += dv_write_pack((c->sys->dsf ? dv_header625 : dv_header525),
  988. c, buf);
  989. buf += 72; /* unused bytes */
  990. /* DV subcode: 2DIFs */
  991. for (j = 0; j < 2; j++) {
  992. buf += dv_write_dif_id(dv_sect_subcode, chan+chan_offset, i, j, buf);
  993. for (k = 0; k < 6; k++)
  994. buf += dv_write_ssyb_id(k, (i < c->sys->difseg_size / 2), buf) + 5;
  995. buf += 29; /* unused bytes */
  996. }
  997. /* DV VAUX: 3DIFS */
  998. for (j = 0; j < 3; j++) {
  999. buf += dv_write_dif_id(dv_sect_vaux, chan+chan_offset, i, j, buf);
  1000. buf += dv_write_pack(dv_video_source, c, buf);
  1001. buf += dv_write_pack(dv_video_control, c, buf);
  1002. buf += 7 * 5;
  1003. buf += dv_write_pack(dv_video_source, c, buf);
  1004. buf += dv_write_pack(dv_video_control, c, buf);
  1005. buf += 4 * 5 + 2; /* unused bytes */
  1006. }
  1007. /* DV Audio/Video: 135 Video DIFs + 9 Audio DIFs */
  1008. for (j = 0; j < 135; j++) {
  1009. if (j % 15 == 0) {
  1010. memset(buf, 0xff, 80);
  1011. buf += dv_write_dif_id(dv_sect_audio, chan+chan_offset, i, j/15, buf);
  1012. buf += 77; /* audio control & shuffled PCM audio */
  1013. }
  1014. buf += dv_write_dif_id(dv_sect_video, chan+chan_offset, i, j, buf);
  1015. buf += 77; /* 1 video macroblock: 1 bytes control
  1016. * 4 * 14 bytes Y 8x8 data
  1017. * 10 bytes Cr 8x8 data
  1018. * 10 bytes Cb 8x8 data */
  1019. }
  1020. }
  1021. }
  1022. }
  1023. static int dvvideo_encode_frame(AVCodecContext *c, AVPacket *pkt,
  1024. const AVFrame *frame, int *got_packet)
  1025. {
  1026. DVVideoContext *s = c->priv_data;
  1027. int ret;
  1028. if ((ret = ff_alloc_packet2(c, pkt, s->sys->frame_size, 0)) < 0)
  1029. return ret;
  1030. c->pix_fmt = s->sys->pix_fmt;
  1031. s->frame = frame;
  1032. #if FF_API_CODED_FRAME
  1033. FF_DISABLE_DEPRECATION_WARNINGS
  1034. c->coded_frame->key_frame = 1;
  1035. c->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  1036. FF_ENABLE_DEPRECATION_WARNINGS
  1037. #endif
  1038. s->buf = pkt->data;
  1039. dv_format_frame(s, pkt->data);
  1040. c->execute(c, dv_encode_video_segment, s->work_chunks, NULL,
  1041. dv_work_pool_size(s->sys), sizeof(DVwork_chunk));
  1042. emms_c();
  1043. pkt->flags |= AV_PKT_FLAG_KEY;
  1044. *got_packet = 1;
  1045. return 0;
  1046. }
  1047. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  1048. #define OFFSET(x) offsetof(DVVideoContext, x)
  1049. static const AVOption dv_options[] = {
  1050. { "quant_deadzone", "Quantizer dead zone", OFFSET(quant_deadzone), AV_OPT_TYPE_INT, { .i64 = 7 }, 0, 1024, VE },
  1051. { NULL },
  1052. };
  1053. static const AVClass dvvideo_encode_class = {
  1054. .class_name = "dvvideo encoder",
  1055. .item_name = av_default_item_name,
  1056. .option = dv_options,
  1057. .version = LIBAVUTIL_VERSION_INT,
  1058. };
  1059. AVCodec ff_dvvideo_encoder = {
  1060. .name = "dvvideo",
  1061. .long_name = NULL_IF_CONFIG_SMALL("DV (Digital Video)"),
  1062. .type = AVMEDIA_TYPE_VIDEO,
  1063. .id = AV_CODEC_ID_DVVIDEO,
  1064. .priv_data_size = sizeof(DVVideoContext),
  1065. .init = dvvideo_encode_init,
  1066. .encode2 = dvvideo_encode_frame,
  1067. .capabilities = AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS,
  1068. .pix_fmts = (const enum AVPixelFormat[]) {
  1069. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV422P,
  1070. AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE
  1071. },
  1072. .priv_class = &dvvideo_encode_class,
  1073. };