dvenc.c 46 KB

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