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.

1231 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^(dv100_weight_shift)
  287. (in DV100 the AC components are divided by the spec weights) */
  288. static const int dv100_weight_shift = 16;
  289. static const int dv_weight_1080[2][64] = {
  290. { 8192, 65536, 65536, 61681, 61681, 61681, 58254, 58254,
  291. 58254, 58254, 58254, 58254, 55188, 58254, 58254, 55188,
  292. 55188, 55188, 55188, 55188, 55188, 24966, 27594, 26214,
  293. 26214, 26214, 27594, 24966, 23831, 24385, 25575, 25575,
  294. 25575, 25575, 24385, 23831, 23302, 23302, 24966, 24966,
  295. 24966, 23302, 23302, 21845, 22795, 24385, 24385, 22795,
  296. 21845, 21400, 21845, 23831, 21845, 21400, 10382, 10700,
  297. 10700, 10382, 10082, 9620, 10082, 9039, 9039, 8525, },
  298. { 8192, 65536, 65536, 61681, 61681, 61681, 41943, 41943,
  299. 41943, 41943, 40330, 41943, 40330, 41943, 40330, 40330,
  300. 40330, 38836, 38836, 40330, 40330, 24966, 27594, 26214,
  301. 26214, 26214, 27594, 24966, 23831, 24385, 25575, 25575,
  302. 25575, 25575, 24385, 23831, 11523, 11523, 12483, 12483,
  303. 12483, 11523, 11523, 10923, 11275, 12193, 12193, 11275,
  304. 10923, 5323, 5490, 5924, 5490, 5323, 5165, 5323,
  305. 5323, 5165, 5017, 4788, 5017, 4520, 4520, 4263, }
  306. };
  307. static const int dv_weight_720[2][64] = {
  308. { 8192, 65536, 65536, 61681, 61681, 61681, 58254, 58254,
  309. 58254, 58254, 58254, 58254, 55188, 58254, 58254, 55188,
  310. 55188, 55188, 55188, 55188, 55188, 24966, 27594, 26214,
  311. 26214, 26214, 27594, 24966, 23831, 24385, 25575, 25575,
  312. 25575, 25575, 24385, 23831, 15420, 15420, 16644, 16644,
  313. 16644, 15420, 15420, 10923, 11398, 12193, 12193, 11398,
  314. 10923, 10700, 10923, 11916, 10923, 10700, 5191, 5350,
  315. 5350, 5191, 5041, 4810, 5041, 4520, 4520, 4263, },
  316. { 8192, 43691, 43691, 40330, 40330, 40330, 29127, 29127,
  317. 29127, 29127, 29127, 29127, 27594, 29127, 29127, 27594,
  318. 27594, 27594, 27594, 27594, 27594, 12483, 13797, 13107,
  319. 13107, 13107, 13797, 12483, 11916, 12193, 12788, 12788,
  320. 12788, 12788, 12193, 11916, 5761, 5761, 6242, 6242,
  321. 6242, 5761, 5761, 5461, 5638, 5461, 6096, 5638,
  322. 5461, 2661, 2745, 2962, 2745, 2661, 2583, 2661,
  323. 2661, 2583, 2509, 2394, 2509, 2260, 2260, 2131, }
  324. };
  325. static av_always_inline int dv_set_class_number_sd(DVVideoContext *s,
  326. int16_t *blk, EncBlockInfo *bi,
  327. const uint8_t *zigzag_scan,
  328. const int *weight, int bias)
  329. {
  330. int i, area;
  331. /* We offer two different methods for class number assignment: the
  332. * method suggested in SMPTE 314M Table 22, and an improved
  333. * method. The SMPTE method is very conservative; it assigns class
  334. * 3 (i.e. severe quantization) to any block where the largest AC
  335. * component is greater than 36. FFmpeg's DV encoder tracks AC bit
  336. * consumption precisely, so there is no need to bias most blocks
  337. * towards strongly lossy compression. Instead, we assign class 2
  338. * to most blocks, and use class 3 only when strictly necessary
  339. * (for blocks whose largest AC component exceeds 255). */
  340. #if 0 /* SMPTE spec method */
  341. static const int classes[] = { 12, 24, 36, 0xffff };
  342. #else /* improved FFmpeg method */
  343. static const int classes[] = { -1, -1, 255, 0xffff };
  344. #endif
  345. int max = classes[0];
  346. int prev = 0;
  347. const unsigned deadzone = s->quant_deadzone;
  348. const unsigned threshold = 2 * deadzone;
  349. bi->mb[0] = blk[0];
  350. for (area = 0; area < 4; area++) {
  351. bi->prev[area] = prev;
  352. bi->bit_size[area] = 1; // 4 areas 4 bits for EOB :)
  353. for (i = mb_area_start[area]; i < mb_area_start[area + 1]; i++) {
  354. int level = blk[zigzag_scan[i]];
  355. if (level + deadzone > threshold) {
  356. bi->sign[i] = (level >> 31) & 1;
  357. /* Weight it and shift down into range, adding for rounding.
  358. * The extra division by a factor of 2^4 reverses the 8x
  359. * expansion of the DCT AND the 2x doubling of the weights. */
  360. level = (FFABS(level) * weight[i] + (1 << (dv_weight_bits + 3))) >>
  361. (dv_weight_bits + 4);
  362. if (!level)
  363. continue;
  364. bi->mb[i] = level;
  365. if (level > max)
  366. max = level;
  367. bi->bit_size[area] += dv_rl2vlc_size(i - prev - 1, level);
  368. bi->next[prev] = i;
  369. prev = i;
  370. }
  371. }
  372. }
  373. bi->next[prev] = i;
  374. for (bi->cno = 0; max > classes[bi->cno]; bi->cno++)
  375. ;
  376. bi->cno += bias;
  377. if (bi->cno >= 3) {
  378. bi->cno = 3;
  379. prev = 0;
  380. i = bi->next[prev];
  381. for (area = 0; area < 4; area++) {
  382. bi->prev[area] = prev;
  383. bi->bit_size[area] = 1; // 4 areas 4 bits for EOB :)
  384. for (; i < mb_area_start[area + 1]; i = bi->next[i]) {
  385. bi->mb[i] >>= 1;
  386. if (bi->mb[i]) {
  387. bi->bit_size[area] += dv_rl2vlc_size(i - prev - 1, bi->mb[i]);
  388. bi->next[prev] = i;
  389. prev = i;
  390. }
  391. }
  392. }
  393. bi->next[prev] = i;
  394. }
  395. return bi->bit_size[0] + bi->bit_size[1] +
  396. bi->bit_size[2] + bi->bit_size[3];
  397. }
  398. /* this function just copies the DCT coefficients and performs
  399. the initial (non-)quantization. */
  400. static inline void dv_set_class_number_hd(DVVideoContext *s,
  401. int16_t *blk, EncBlockInfo *bi,
  402. const uint8_t *zigzag_scan,
  403. const int *weight, int bias)
  404. {
  405. int i, max = 0;
  406. /* the first quantization (none at all) */
  407. bi->area_q[0] = 1;
  408. /* weigh AC components and store to save[] */
  409. /* (i=0 is the DC component; we only include it to make the
  410. number of loop iterations even, for future possible SIMD optimization) */
  411. for (i = 0; i < 64; i += 2) {
  412. int level0, level1;
  413. /* get the AC component (in zig-zag order) */
  414. level0 = blk[zigzag_scan[i+0]];
  415. level1 = blk[zigzag_scan[i+1]];
  416. /* extract sign and make it the lowest bit */
  417. bi->sign[i+0] = (level0>>31)&1;
  418. bi->sign[i+1] = (level1>>31)&1;
  419. /* take absolute value of the level */
  420. level0 = FFABS(level0);
  421. level1 = FFABS(level1);
  422. /* weigh it */
  423. level0 = (level0*weight[i+0] + 4096 + (1<<17)) >> 18;
  424. level1 = (level1*weight[i+1] + 4096 + (1<<17)) >> 18;
  425. /* save unquantized value */
  426. bi->save[i+0] = level0;
  427. bi->save[i+1] = level1;
  428. /* find max component */
  429. if (bi->save[i+0] > max)
  430. max = bi->save[i+0];
  431. if (bi->save[i+1] > max)
  432. max = bi->save[i+1];
  433. }
  434. /* copy DC component */
  435. bi->mb[0] = blk[0];
  436. /* the EOB code is 4 bits */
  437. bi->bit_size[0] = 4;
  438. bi->bit_size[1] = bi->bit_size[2] = bi->bit_size[3] = 0;
  439. /* ensure that no AC coefficients are cut off */
  440. bi->min_qlevel = ((max+256) >> 8);
  441. bi->area_q[0] = 25; /* set to an "impossible" value */
  442. bi->cno = 0;
  443. }
  444. static av_always_inline int dv_init_enc_block(EncBlockInfo* bi, uint8_t *data, int linesize,
  445. DVVideoContext *s, int chroma)
  446. {
  447. LOCAL_ALIGNED_16(int16_t, blk, [64]);
  448. bi->area_q[0] = bi->area_q[1] = bi->area_q[2] = bi->area_q[3] = 0;
  449. bi->partial_bit_count = 0;
  450. bi->partial_bit_buffer = 0;
  451. bi->cur_ac = 0;
  452. if (data) {
  453. if (DV_PROFILE_IS_HD(s->sys)) {
  454. s->get_pixels(blk, data, linesize * (1 << bi->dct_mode));
  455. s->fdct[0](blk);
  456. } else {
  457. bi->dct_mode = dv_guess_dct_mode(s, data, linesize);
  458. s->get_pixels(blk, data, linesize);
  459. s->fdct[bi->dct_mode](blk);
  460. }
  461. } else {
  462. /* We rely on the fact that encoding all zeros leads to an immediate EOB,
  463. which is precisely what the spec calls for in the "dummy" blocks. */
  464. memset(blk, 0, 64*sizeof(*blk));
  465. bi->dct_mode = 0;
  466. }
  467. if (DV_PROFILE_IS_HD(s->sys)) {
  468. const int *weights;
  469. if (s->sys->height == 1080) {
  470. weights = dv_weight_1080[chroma];
  471. } else { /* 720p */
  472. weights = dv_weight_720[chroma];
  473. }
  474. dv_set_class_number_hd(s, blk, bi,
  475. ff_zigzag_direct,
  476. weights,
  477. dv100_min_bias+chroma*dv100_chroma_bias);
  478. } else {
  479. dv_set_class_number_sd(s, blk, bi,
  480. bi->dct_mode ? ff_dv_zigzag248_direct : ff_zigzag_direct,
  481. bi->dct_mode ? dv_weight_248 : dv_weight_88,
  482. chroma);
  483. }
  484. return bi->bit_size[0] + bi->bit_size[1] + bi->bit_size[2] + bi->bit_size[3];
  485. }
  486. /* DV100 quantize
  487. Perform quantization by divinding the AC component by the qstep.
  488. As an optimization we use a fixed-point integer multiply instead
  489. of a divide. */
  490. static av_always_inline int dv100_quantize(int level, int qsinv)
  491. {
  492. /* this code is equivalent to */
  493. /* return (level + qs/2) / qs; */
  494. return (level * qsinv + 1024 + (1<<(dv100_qstep_bits-1))) >> dv100_qstep_bits;
  495. /* the extra +1024 is needed to make the rounding come out right. */
  496. /* I (DJM) have verified that the results are exactly the same as
  497. division for level 0-2048 at all QNOs. */
  498. }
  499. static int dv100_actual_quantize(EncBlockInfo *b, int qlevel)
  500. {
  501. int prev, k, qsinv;
  502. int qno = DV100_QLEVEL_QNO(dv100_qlevels[qlevel]);
  503. int cno = DV100_QLEVEL_CNO(dv100_qlevels[qlevel]);
  504. if (b->area_q[0] == qno && b->cno == cno)
  505. return b->bit_size[0];
  506. qsinv = dv100_qstep_inv[qno];
  507. /* record the new qstep */
  508. b->area_q[0] = qno;
  509. b->cno = cno;
  510. /* reset encoded size (EOB = 4 bits) */
  511. b->bit_size[0] = 4;
  512. /* visit nonzero components and quantize */
  513. prev = 0;
  514. for (k = 1; k < 64; k++) {
  515. /* quantize */
  516. int ac = dv100_quantize(b->save[k], qsinv) >> cno;
  517. if (ac) {
  518. if (ac > 255)
  519. ac = 255;
  520. b->mb[k] = ac;
  521. b->bit_size[0] += dv_rl2vlc_size(k - prev - 1, ac);
  522. b->next[prev] = k;
  523. prev = k;
  524. }
  525. }
  526. b->next[prev] = k;
  527. return b->bit_size[0];
  528. }
  529. static inline void dv_guess_qnos_hd(EncBlockInfo *blks, int *qnos)
  530. {
  531. EncBlockInfo *b;
  532. int min_qlevel[5];
  533. int qlevels[5];
  534. int size[5];
  535. int i, j;
  536. /* cache block sizes at hypothetical qlevels */
  537. uint16_t size_cache[5*8][DV100_NUM_QLEVELS] = {{0}};
  538. /* get minimum qlevels */
  539. for (i = 0; i < 5; i++) {
  540. min_qlevel[i] = 1;
  541. for (j = 0; j < 8; j++) {
  542. if (blks[8*i+j].min_qlevel > min_qlevel[i])
  543. min_qlevel[i] = blks[8*i+j].min_qlevel;
  544. }
  545. }
  546. /* initialize sizes */
  547. for (i = 0; i < 5; i++) {
  548. qlevels[i] = dv100_starting_qno;
  549. if (qlevels[i] < min_qlevel[i])
  550. qlevels[i] = min_qlevel[i];
  551. qnos[i] = DV100_QLEVEL_QNO(dv100_qlevels[qlevels[i]]);
  552. size[i] = 0;
  553. for (j = 0; j < 8; j++) {
  554. size_cache[8*i+j][qlevels[i]] = dv100_actual_quantize(&blks[8*i+j], qlevels[i]);
  555. size[i] += size_cache[8*i+j][qlevels[i]];
  556. }
  557. }
  558. /* must we go coarser? */
  559. if (size[0]+size[1]+size[2]+size[3]+size[4] > vs_total_ac_bits_hd) {
  560. int largest = size[0] % 5; /* 'random' number */
  561. int qlevels_done = 0;
  562. do {
  563. /* find the macroblock with the lowest qlevel */
  564. for (i = 0; i < 5; i++) {
  565. if (qlevels[i] < qlevels[largest])
  566. largest = i;
  567. }
  568. i = largest;
  569. /* ensure that we don't enter infinite loop */
  570. largest = (largest+1) % 5;
  571. /* quantize a little bit more */
  572. qlevels[i] += dv100_qlevel_inc;
  573. if (qlevels[i] > DV100_NUM_QLEVELS-1) {
  574. qlevels[i] = DV100_NUM_QLEVELS-1;
  575. qlevels_done++;
  576. }
  577. qnos[i] = DV100_QLEVEL_QNO(dv100_qlevels[qlevels[i]]);
  578. size[i] = 0;
  579. /* for each block */
  580. b = &blks[8*i];
  581. for (j = 0; j < 8; j++, b++) {
  582. /* accumulate block size into macroblock */
  583. if(size_cache[8*i+j][qlevels[i]] == 0) {
  584. /* it is safe to use actual_quantize() here because we only go from finer to coarser,
  585. and it saves the final actual_quantize() down below */
  586. size_cache[8*i+j][qlevels[i]] = dv100_actual_quantize(b, qlevels[i]);
  587. }
  588. size[i] += size_cache[8*i+j][qlevels[i]];
  589. } /* for each block */
  590. } while (vs_total_ac_bits_hd < size[0] + size[1] + size[2] + size[3] + size[4] && qlevels_done < 5);
  591. // can we go finer?
  592. } else if (DV100_ENABLE_FINER &&
  593. size[0]+size[1]+size[2]+size[3]+size[4] < vs_total_ac_bits_hd) {
  594. int save_qlevel;
  595. int largest = size[0] % 5; /* 'random' number */
  596. while (qlevels[0] > min_qlevel[0] ||
  597. qlevels[1] > min_qlevel[1] ||
  598. qlevels[2] > min_qlevel[2] ||
  599. qlevels[3] > min_qlevel[3] ||
  600. qlevels[4] > min_qlevel[4]) {
  601. /* find the macroblock with the highest qlevel */
  602. for (i = 0; i < 5; i++) {
  603. if (qlevels[i] > min_qlevel[i] && qlevels[i] > qlevels[largest])
  604. largest = i;
  605. }
  606. i = largest;
  607. /* ensure that we don't enter infinite loop */
  608. largest = (largest+1) % 5;
  609. if (qlevels[i] <= min_qlevel[i]) {
  610. /* can't unquantize any more */
  611. continue;
  612. }
  613. /* quantize a little bit less */
  614. save_qlevel = qlevels[i];
  615. qlevels[i] -= dv100_qlevel_inc;
  616. if (qlevels[i] < min_qlevel[i])
  617. qlevels[i] = min_qlevel[i];
  618. qnos[i] = DV100_QLEVEL_QNO(dv100_qlevels[qlevels[i]]);
  619. size[i] = 0;
  620. /* for each block */
  621. b = &blks[8*i];
  622. for (j = 0; j < 8; j++, b++) {
  623. /* accumulate block size into macroblock */
  624. if(size_cache[8*i+j][qlevels[i]] == 0) {
  625. size_cache[8*i+j][qlevels[i]] = dv100_actual_quantize(b, qlevels[i]);
  626. }
  627. size[i] += size_cache[8*i+j][qlevels[i]];
  628. } /* for each block */
  629. /* did we bust the limit? */
  630. if (vs_total_ac_bits_hd < size[0] + size[1] + size[2] + size[3] + size[4]) {
  631. /* go back down and exit */
  632. qlevels[i] = save_qlevel;
  633. qnos[i] = DV100_QLEVEL_QNO(dv100_qlevels[qlevels[i]]);
  634. break;
  635. }
  636. }
  637. }
  638. /* now do the actual quantization */
  639. for (i = 0; i < 5; i++) {
  640. /* for each block */
  641. b = &blks[8*i];
  642. size[i] = 0;
  643. for (j = 0; j < 8; j++, b++) {
  644. /* accumulate block size into macroblock */
  645. size[i] += dv100_actual_quantize(b, qlevels[i]);
  646. } /* for each block */
  647. }
  648. }
  649. static inline void dv_guess_qnos(EncBlockInfo *blks, int *qnos)
  650. {
  651. int size[5];
  652. int i, j, k, a, prev, a2;
  653. EncBlockInfo *b;
  654. size[0] =
  655. size[1] =
  656. size[2] =
  657. size[3] =
  658. size[4] = 1 << 24;
  659. do {
  660. b = blks;
  661. for (i = 0; i < 5; i++) {
  662. if (!qnos[i])
  663. continue;
  664. qnos[i]--;
  665. size[i] = 0;
  666. for (j = 0; j < 6; j++, b++) {
  667. for (a = 0; a < 4; a++) {
  668. if (b->area_q[a] != ff_dv_quant_shifts[qnos[i] + ff_dv_quant_offset[b->cno]][a]) {
  669. b->bit_size[a] = 1; // 4 areas 4 bits for EOB :)
  670. b->area_q[a]++;
  671. prev = b->prev[a];
  672. av_assert2(b->next[prev] >= mb_area_start[a + 1] || b->mb[prev]);
  673. for (k = b->next[prev]; k < mb_area_start[a + 1]; k = b->next[k]) {
  674. b->mb[k] >>= 1;
  675. if (b->mb[k]) {
  676. b->bit_size[a] += dv_rl2vlc_size(k - prev - 1, b->mb[k]);
  677. prev = k;
  678. } else {
  679. if (b->next[k] >= mb_area_start[a + 1] && b->next[k] < 64) {
  680. for (a2 = a + 1; b->next[k] >= mb_area_start[a2 + 1]; a2++)
  681. b->prev[a2] = prev;
  682. av_assert2(a2 < 4);
  683. av_assert2(b->mb[b->next[k]]);
  684. b->bit_size[a2] += dv_rl2vlc_size(b->next[k] - prev - 1, b->mb[b->next[k]]) -
  685. dv_rl2vlc_size(b->next[k] - k - 1, b->mb[b->next[k]]);
  686. av_assert2(b->prev[a2] == k && (a2 + 1 >= 4 || b->prev[a2 + 1] != k));
  687. b->prev[a2] = prev;
  688. }
  689. b->next[prev] = b->next[k];
  690. }
  691. }
  692. b->prev[a + 1] = prev;
  693. }
  694. size[i] += b->bit_size[a];
  695. }
  696. }
  697. if (vs_total_ac_bits >= size[0] + size[1] + size[2] + size[3] + size[4])
  698. return;
  699. }
  700. } while (qnos[0] | qnos[1] | qnos[2] | qnos[3] | qnos[4]);
  701. for (a = 2; a == 2 || vs_total_ac_bits < size[0]; a += a) {
  702. b = blks;
  703. size[0] = 5 * 6 * 4; // EOB
  704. for (j = 0; j < 6 * 5; j++, b++) {
  705. prev = b->prev[0];
  706. for (k = b->next[prev]; k < 64; k = b->next[k]) {
  707. if (b->mb[k] < a && b->mb[k] > -a) {
  708. b->next[prev] = b->next[k];
  709. } else {
  710. size[0] += dv_rl2vlc_size(k - prev - 1, b->mb[k]);
  711. prev = k;
  712. }
  713. }
  714. }
  715. }
  716. }
  717. /* update all cno values into the blocks, over-writing the old values without
  718. touching anything else. (only used for DV100) */
  719. static inline void dv_revise_cnos(uint8_t *dif, EncBlockInfo *blk, const AVDVProfile *profile)
  720. {
  721. uint8_t *data;
  722. int mb_index, i;
  723. for (mb_index = 0; mb_index < 5; mb_index++) {
  724. data = dif + mb_index*80 + 4;
  725. for (i = 0; i < profile->bpm; i++) {
  726. /* zero out the class number */
  727. data[1] &= 0xCF;
  728. /* add the new one */
  729. data[1] |= blk[profile->bpm*mb_index+i].cno << 4;
  730. data += profile->block_sizes[i] >> 3;
  731. }
  732. }
  733. }
  734. static int dv_encode_video_segment(AVCodecContext *avctx, void *arg)
  735. {
  736. DVVideoContext *s = avctx->priv_data;
  737. DVwork_chunk *work_chunk = arg;
  738. int mb_index, i, j;
  739. int mb_x, mb_y, c_offset;
  740. ptrdiff_t linesize, y_stride;
  741. uint8_t *y_ptr;
  742. uint8_t *dif, *p;
  743. LOCAL_ALIGNED_8(uint8_t, scratch, [128]);
  744. EncBlockInfo enc_blks[5 * DV_MAX_BPM];
  745. PutBitContext pbs[5 * DV_MAX_BPM];
  746. PutBitContext *pb;
  747. EncBlockInfo *enc_blk;
  748. int vs_bit_size = 0;
  749. int qnos[5];
  750. int *qnosp = &qnos[0];
  751. p = dif = &s->buf[work_chunk->buf_offset * 80];
  752. enc_blk = &enc_blks[0];
  753. for (mb_index = 0; mb_index < 5; mb_index++) {
  754. dv_calculate_mb_xy(s, work_chunk, mb_index, &mb_x, &mb_y);
  755. qnos[mb_index] = DV_PROFILE_IS_HD(s->sys) ? 1 : 15;
  756. y_ptr = s->frame->data[0] + (mb_y * s->frame->linesize[0] + mb_x) * 8;
  757. linesize = s->frame->linesize[0];
  758. if (s->sys->height == 1080 && mb_y < 134)
  759. enc_blk->dct_mode = dv_guess_dct_mode(s, y_ptr, linesize);
  760. else
  761. enc_blk->dct_mode = 0;
  762. for (i = 1; i < 8; i++)
  763. enc_blk[i].dct_mode = enc_blk->dct_mode;
  764. /* initializing luminance blocks */
  765. if ((s->sys->pix_fmt == AV_PIX_FMT_YUV420P) ||
  766. (s->sys->pix_fmt == AV_PIX_FMT_YUV411P && mb_x >= (704 / 8)) ||
  767. (s->sys->height >= 720 && mb_y != 134)) {
  768. y_stride = s->frame->linesize[0] * (1 << (3*!enc_blk->dct_mode));
  769. } else {
  770. y_stride = 16;
  771. }
  772. y_ptr = s->frame->data[0] +
  773. (mb_y * s->frame->linesize[0] + mb_x) * 8;
  774. linesize = s->frame->linesize[0];
  775. if (s->sys->video_stype == 4) { /* SD 422 */
  776. vs_bit_size +=
  777. dv_init_enc_block(enc_blk + 0, y_ptr, linesize, s, 0) +
  778. dv_init_enc_block(enc_blk + 1, NULL, linesize, s, 0) +
  779. dv_init_enc_block(enc_blk + 2, y_ptr + 8, linesize, s, 0) +
  780. dv_init_enc_block(enc_blk + 3, NULL, linesize, s, 0);
  781. } else {
  782. vs_bit_size +=
  783. dv_init_enc_block(enc_blk + 0, y_ptr, linesize, s, 0) +
  784. dv_init_enc_block(enc_blk + 1, y_ptr + 8, linesize, s, 0) +
  785. dv_init_enc_block(enc_blk + 2, y_ptr + y_stride, linesize, s, 0) +
  786. dv_init_enc_block(enc_blk + 3, y_ptr + 8 + y_stride, linesize, s, 0);
  787. }
  788. enc_blk += 4;
  789. /* initializing chrominance blocks */
  790. c_offset = ((mb_y >> (s->sys->pix_fmt == AV_PIX_FMT_YUV420P)) * s->frame->linesize[1] +
  791. (mb_x >> ((s->sys->pix_fmt == AV_PIX_FMT_YUV411P) ? 2 : 1))) * 8;
  792. for (j = 2; j; j--) {
  793. uint8_t *c_ptr = s->frame->data[j] + c_offset;
  794. linesize = s->frame->linesize[j];
  795. y_stride = (mb_y == 134) ? 8 : (s->frame->linesize[j] * (1 << (3*!enc_blk->dct_mode)));
  796. if (s->sys->pix_fmt == AV_PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
  797. uint8_t *d;
  798. uint8_t *b = scratch;
  799. for (i = 0; i < 8; i++) {
  800. d = c_ptr + linesize * 8;
  801. b[0] = c_ptr[0];
  802. b[1] = c_ptr[1];
  803. b[2] = c_ptr[2];
  804. b[3] = c_ptr[3];
  805. b[4] = d[0];
  806. b[5] = d[1];
  807. b[6] = d[2];
  808. b[7] = d[3];
  809. c_ptr += linesize;
  810. b += 16;
  811. }
  812. c_ptr = scratch;
  813. linesize = 16;
  814. }
  815. vs_bit_size += dv_init_enc_block(enc_blk++, c_ptr, linesize, s, 1);
  816. if (s->sys->bpm == 8)
  817. vs_bit_size += dv_init_enc_block(enc_blk++, c_ptr + y_stride,
  818. linesize, s, 1);
  819. }
  820. }
  821. if (DV_PROFILE_IS_HD(s->sys)) {
  822. /* unconditional */
  823. dv_guess_qnos_hd(&enc_blks[0], qnosp);
  824. } else if (vs_total_ac_bits < vs_bit_size) {
  825. dv_guess_qnos(&enc_blks[0], qnosp);
  826. }
  827. /* DIF encoding process */
  828. for (j = 0; j < 5 * s->sys->bpm;) {
  829. int start_mb = j;
  830. p[3] = *qnosp++;
  831. p += 4;
  832. /* First pass over individual cells only */
  833. for (i = 0; i < s->sys->bpm; i++, j++) {
  834. int sz = s->sys->block_sizes[i] >> 3;
  835. init_put_bits(&pbs[j], p, sz);
  836. put_sbits(&pbs[j], 9, ((enc_blks[j].mb[0] >> 3) - 1024 + 2) >> 2);
  837. put_bits(&pbs[j], 1, DV_PROFILE_IS_HD(s->sys) && i ? 1 : enc_blks[j].dct_mode);
  838. put_bits(&pbs[j], 2, enc_blks[j].cno);
  839. dv_encode_ac(&enc_blks[j], &pbs[j], &pbs[j + 1]);
  840. p += sz;
  841. }
  842. /* Second pass over each MB space */
  843. pb = &pbs[start_mb];
  844. for (i = 0; i < s->sys->bpm; i++)
  845. if (enc_blks[start_mb + i].partial_bit_count)
  846. pb = dv_encode_ac(&enc_blks[start_mb + i], pb,
  847. &pbs[start_mb + s->sys->bpm]);
  848. }
  849. /* Third and final pass over the whole video segment space */
  850. pb = &pbs[0];
  851. for (j = 0; j < 5 * s->sys->bpm; j++) {
  852. if (enc_blks[j].partial_bit_count)
  853. pb = dv_encode_ac(&enc_blks[j], pb, &pbs[s->sys->bpm * 5]);
  854. if (enc_blks[j].partial_bit_count)
  855. av_log(avctx, AV_LOG_ERROR, "ac bitstream overflow\n");
  856. }
  857. for (j = 0; j < 5 * s->sys->bpm; j++) {
  858. int pos;
  859. int size = pbs[j].size_in_bits >> 3;
  860. flush_put_bits(&pbs[j]);
  861. pos = put_bits_count(&pbs[j]) >> 3;
  862. if (pos > size) {
  863. av_log(avctx, AV_LOG_ERROR,
  864. "bitstream written beyond buffer size\n");
  865. return -1;
  866. }
  867. memset(pbs[j].buf + pos, 0xff, size - pos);
  868. }
  869. if (DV_PROFILE_IS_HD(s->sys))
  870. dv_revise_cnos(dif, enc_blks, s->sys);
  871. return 0;
  872. }
  873. static inline int dv_write_pack(enum dv_pack_type pack_id, DVVideoContext *c,
  874. uint8_t *buf)
  875. {
  876. /*
  877. * Here's what SMPTE314M says about these two:
  878. * (page 6) APTn, AP1n, AP2n, AP3n: These data shall be identical
  879. * as track application IDs (APTn = 001, AP1n =
  880. * 001, AP2n = 001, AP3n = 001), if the source signal
  881. * comes from a digital VCR. If the signal source is
  882. * unknown, all bits for these data shall be set to 1.
  883. * (page 12) STYPE: STYPE defines a signal type of video signal
  884. * 00000b = 4:1:1 compression
  885. * 00100b = 4:2:2 compression
  886. * XXXXXX = Reserved
  887. * Now, I've got two problems with these statements:
  888. * 1. it looks like APT == 111b should be a safe bet, but it isn't.
  889. * It seems that for PAL as defined in IEC 61834 we have to set
  890. * APT to 000 and for SMPTE314M to 001.
  891. * 2. It is not at all clear what STYPE is used for 4:2:0 PAL
  892. * compression scheme (if any).
  893. */
  894. uint8_t aspect = 0;
  895. int apt = (c->sys->pix_fmt == AV_PIX_FMT_YUV420P ? 0 : 1);
  896. int fs;
  897. if (c->avctx->height >= 720)
  898. fs = c->avctx->height == 720 || c->frame->top_field_first ? 0x40 : 0x00;
  899. else
  900. fs = c->frame->top_field_first ? 0x00 : 0x40;
  901. if (DV_PROFILE_IS_HD(c->sys) ||
  902. (int)(av_q2d(c->avctx->sample_aspect_ratio) *
  903. c->avctx->width / c->avctx->height * 10) >= 17)
  904. /* HD formats are always 16:9 */
  905. aspect = 0x02;
  906. buf[0] = (uint8_t) pack_id;
  907. switch (pack_id) {
  908. case dv_header525: /* I can't imagine why these two weren't defined as real */
  909. case dv_header625: /* packs in SMPTE314M -- they definitely look like ones */
  910. buf[1] = 0xf8 | /* reserved -- always 1 */
  911. (apt & 0x07); /* APT: Track application ID */
  912. buf[2] = (0 << 7) | /* TF1: audio data is 0 - valid; 1 - invalid */
  913. (0x0f << 3) | /* reserved -- always 1 */
  914. (apt & 0x07); /* AP1: Audio application ID */
  915. buf[3] = (0 << 7) | /* TF2: video data is 0 - valid; 1 - invalid */
  916. (0x0f << 3) | /* reserved -- always 1 */
  917. (apt & 0x07); /* AP2: Video application ID */
  918. buf[4] = (0 << 7) | /* TF3: subcode(SSYB) is 0 - valid; 1 - invalid */
  919. (0x0f << 3) | /* reserved -- always 1 */
  920. (apt & 0x07); /* AP3: Subcode application ID */
  921. break;
  922. case dv_video_source:
  923. buf[1] = 0xff; /* reserved -- always 1 */
  924. buf[2] = (1 << 7) | /* B/W: 0 - b/w, 1 - color */
  925. (1 << 6) | /* following CLF is valid - 0, invalid - 1 */
  926. (3 << 4) | /* CLF: color frames ID (see ITU-R BT.470-4) */
  927. 0xf; /* reserved -- always 1 */
  928. buf[3] = (3 << 6) | /* reserved -- always 1 */
  929. (c->sys->dsf << 5) | /* system: 60fields/50fields */
  930. c->sys->video_stype; /* signal type video compression */
  931. buf[4] = 0xff; /* VISC: 0xff -- no information */
  932. break;
  933. case dv_video_control:
  934. buf[1] = (0 << 6) | /* Copy generation management (CGMS) 0 -- free */
  935. 0x3f; /* reserved -- always 1 */
  936. buf[2] = 0xc8 | /* reserved -- always b11001xxx */
  937. aspect;
  938. buf[3] = (1 << 7) | /* frame/field flag 1 -- frame, 0 -- field */
  939. fs | /* first/second field flag 0 -- field 2, 1 -- field 1 */
  940. (1 << 5) | /* frame change flag 0 -- same picture as before, 1 -- different */
  941. (1 << 4) | /* 1 - interlaced, 0 - noninterlaced */
  942. 0xc; /* reserved -- always b1100 */
  943. buf[4] = 0xff; /* reserved -- always 1 */
  944. break;
  945. default:
  946. buf[1] =
  947. buf[2] =
  948. buf[3] =
  949. buf[4] = 0xff;
  950. }
  951. return 5;
  952. }
  953. static inline int dv_write_dif_id(enum dv_section_type t, uint8_t chan_num,
  954. uint8_t seq_num, uint8_t dif_num,
  955. uint8_t *buf)
  956. {
  957. int fsc = chan_num & 1;
  958. int fsp = 1 - (chan_num >> 1);
  959. buf[0] = (uint8_t) t; /* Section type */
  960. buf[1] = (seq_num << 4) | /* DIF seq number 0-9 for 525/60; 0-11 for 625/50 */
  961. (fsc << 3) | /* FSC: for 50 and 100Mb/s 0 - first channel; 1 - second */
  962. (fsp << 2) | /* FSP: for 100Mb/s 1 - channels 0-1; 0 - channels 2-3 */
  963. 3; /* reserved -- always 1 */
  964. buf[2] = dif_num; /* DIF block number Video: 0-134, Audio: 0-8 */
  965. return 3;
  966. }
  967. static inline int dv_write_ssyb_id(uint8_t syb_num, uint8_t fr, uint8_t *buf)
  968. {
  969. if (syb_num == 0 || syb_num == 6) {
  970. buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */
  971. (0 << 4) | /* AP3 (Subcode application ID) */
  972. 0x0f; /* reserved -- always 1 */
  973. } else if (syb_num == 11) {
  974. buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */
  975. 0x7f; /* reserved -- always 1 */
  976. } else {
  977. buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */
  978. (0 << 4) | /* APT (Track application ID) */
  979. 0x0f; /* reserved -- always 1 */
  980. }
  981. buf[1] = 0xf0 | /* reserved -- always 1 */
  982. (syb_num & 0x0f); /* SSYB number 0 - 11 */
  983. buf[2] = 0xff; /* reserved -- always 1 */
  984. return 3;
  985. }
  986. static void dv_format_frame(DVVideoContext *c, uint8_t *buf)
  987. {
  988. int chan, i, j, k;
  989. /* We work with 720p frames split in half. The odd half-frame is chan 2,3 */
  990. int chan_offset = 2*(c->sys->height == 720 && c->avctx->frame_number & 1);
  991. for (chan = 0; chan < c->sys->n_difchan; chan++) {
  992. for (i = 0; i < c->sys->difseg_size; i++) {
  993. memset(buf, 0xff, 80 * 6); /* first 6 DIF blocks are for control data */
  994. /* DV header: 1DIF */
  995. buf += dv_write_dif_id(dv_sect_header, chan+chan_offset, i, 0, buf);
  996. buf += dv_write_pack((c->sys->dsf ? dv_header625 : dv_header525),
  997. c, buf);
  998. buf += 72; /* unused bytes */
  999. /* DV subcode: 2DIFs */
  1000. for (j = 0; j < 2; j++) {
  1001. buf += dv_write_dif_id(dv_sect_subcode, chan+chan_offset, i, j, buf);
  1002. for (k = 0; k < 6; k++)
  1003. buf += dv_write_ssyb_id(k, (i < c->sys->difseg_size / 2), buf) + 5;
  1004. buf += 29; /* unused bytes */
  1005. }
  1006. /* DV VAUX: 3DIFS */
  1007. for (j = 0; j < 3; j++) {
  1008. buf += dv_write_dif_id(dv_sect_vaux, chan+chan_offset, i, j, buf);
  1009. buf += dv_write_pack(dv_video_source, c, buf);
  1010. buf += dv_write_pack(dv_video_control, c, buf);
  1011. buf += 7 * 5;
  1012. buf += dv_write_pack(dv_video_source, c, buf);
  1013. buf += dv_write_pack(dv_video_control, c, buf);
  1014. buf += 4 * 5 + 2; /* unused bytes */
  1015. }
  1016. /* DV Audio/Video: 135 Video DIFs + 9 Audio DIFs */
  1017. for (j = 0; j < 135; j++) {
  1018. if (j % 15 == 0) {
  1019. memset(buf, 0xff, 80);
  1020. buf += dv_write_dif_id(dv_sect_audio, chan+chan_offset, i, j/15, buf);
  1021. buf += 77; /* audio control & shuffled PCM audio */
  1022. }
  1023. buf += dv_write_dif_id(dv_sect_video, chan+chan_offset, i, j, buf);
  1024. buf += 77; /* 1 video macroblock: 1 bytes control
  1025. * 4 * 14 bytes Y 8x8 data
  1026. * 10 bytes Cr 8x8 data
  1027. * 10 bytes Cb 8x8 data */
  1028. }
  1029. }
  1030. }
  1031. }
  1032. static int dvvideo_encode_frame(AVCodecContext *c, AVPacket *pkt,
  1033. const AVFrame *frame, int *got_packet)
  1034. {
  1035. DVVideoContext *s = c->priv_data;
  1036. int ret;
  1037. if ((ret = ff_alloc_packet2(c, pkt, s->sys->frame_size, 0)) < 0)
  1038. return ret;
  1039. c->pix_fmt = s->sys->pix_fmt;
  1040. s->frame = frame;
  1041. #if FF_API_CODED_FRAME
  1042. FF_DISABLE_DEPRECATION_WARNINGS
  1043. c->coded_frame->key_frame = 1;
  1044. c->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  1045. FF_ENABLE_DEPRECATION_WARNINGS
  1046. #endif
  1047. s->buf = pkt->data;
  1048. dv_format_frame(s, pkt->data);
  1049. c->execute(c, dv_encode_video_segment, s->work_chunks, NULL,
  1050. dv_work_pool_size(s->sys), sizeof(DVwork_chunk));
  1051. emms_c();
  1052. pkt->flags |= AV_PKT_FLAG_KEY;
  1053. *got_packet = 1;
  1054. return 0;
  1055. }
  1056. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  1057. #define OFFSET(x) offsetof(DVVideoContext, x)
  1058. static const AVOption dv_options[] = {
  1059. { "quant_deadzone", "Quantizer dead zone", OFFSET(quant_deadzone), AV_OPT_TYPE_INT, { .i64 = 7 }, 0, 1024, VE },
  1060. { NULL },
  1061. };
  1062. static const AVClass dvvideo_encode_class = {
  1063. .class_name = "dvvideo encoder",
  1064. .item_name = av_default_item_name,
  1065. .option = dv_options,
  1066. .version = LIBAVUTIL_VERSION_INT,
  1067. };
  1068. AVCodec ff_dvvideo_encoder = {
  1069. .name = "dvvideo",
  1070. .long_name = NULL_IF_CONFIG_SMALL("DV (Digital Video)"),
  1071. .type = AVMEDIA_TYPE_VIDEO,
  1072. .id = AV_CODEC_ID_DVVIDEO,
  1073. .priv_data_size = sizeof(DVVideoContext),
  1074. .init = dvvideo_encode_init,
  1075. .encode2 = dvvideo_encode_frame,
  1076. .capabilities = AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS,
  1077. .pix_fmts = (const enum AVPixelFormat[]) {
  1078. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV422P,
  1079. AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE
  1080. },
  1081. .priv_class = &dvvideo_encode_class,
  1082. };