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.

1138 lines
42KB

  1. /*
  2. * MPEG1/2 encoder
  3. * Copyright (c) 2000,2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * MPEG1/2 encoder
  25. */
  26. #include <stdint.h>
  27. #include "libavutil/attributes.h"
  28. #include "libavutil/avassert.h"
  29. #include "libavutil/log.h"
  30. #include "libavutil/opt.h"
  31. #include "libavutil/timecode.h"
  32. #include "libavutil/stereo3d.h"
  33. #include "avcodec.h"
  34. #include "bytestream.h"
  35. #include "mathops.h"
  36. #include "mpeg12.h"
  37. #include "mpeg12data.h"
  38. #include "mpegvideo.h"
  39. static const uint8_t inv_non_linear_qscale[] = {
  40. 0, 2, 4, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16,
  41. };
  42. static const uint8_t svcd_scan_offset_placeholder[] = {
  43. 0x10, 0x0E, 0x00, 0x80, 0x81, 0x00, 0x80,
  44. 0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  45. };
  46. static uint8_t mv_penalty[MAX_FCODE + 1][MAX_MV * 2 + 1];
  47. static uint8_t fcode_tab[MAX_MV * 2 + 1];
  48. static uint8_t uni_mpeg1_ac_vlc_len[64 * 64 * 2];
  49. static uint8_t uni_mpeg2_ac_vlc_len[64 * 64 * 2];
  50. /* simple include everything table for dc, first byte is bits
  51. * number next 3 are code */
  52. static uint32_t mpeg1_lum_dc_uni[512];
  53. static uint32_t mpeg1_chr_dc_uni[512];
  54. static uint8_t mpeg1_index_run[2][64];
  55. static int8_t mpeg1_max_level[2][64];
  56. static av_cold void init_uni_ac_vlc(RLTable *rl, uint8_t *uni_ac_vlc_len)
  57. {
  58. int i;
  59. for (i = 0; i < 128; i++) {
  60. int level = i - 64;
  61. int run;
  62. if (!level)
  63. continue;
  64. for (run = 0; run < 64; run++) {
  65. int len, code;
  66. int alevel = FFABS(level);
  67. if (alevel > rl->max_level[0][run])
  68. code = 111; /* rl->n */
  69. else
  70. code = rl->index_run[0][run] + alevel - 1;
  71. if (code < 111) { /* rl->n */
  72. /* length of VLC and sign */
  73. len = rl->table_vlc[code][1] + 1;
  74. } else {
  75. len = rl->table_vlc[111 /* rl->n */][1] + 6;
  76. if (alevel < 128)
  77. len += 8;
  78. else
  79. len += 16;
  80. }
  81. uni_ac_vlc_len[UNI_AC_ENC_INDEX(run, i)] = len;
  82. }
  83. }
  84. }
  85. static int find_frame_rate_index(MpegEncContext *s)
  86. {
  87. int i;
  88. AVRational bestq = (AVRational) {0, 0};
  89. AVRational ext;
  90. AVRational target = av_inv_q(s->avctx->time_base);
  91. for (i = 1; i < 14; i++) {
  92. if (s->avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL &&
  93. i >= 9)
  94. break;
  95. for (ext.num=1; ext.num <= 4; ext.num++) {
  96. for (ext.den=1; ext.den <= 32; ext.den++) {
  97. AVRational q = av_mul_q(ext, ff_mpeg12_frame_rate_tab[i]);
  98. if (s->codec_id != AV_CODEC_ID_MPEG2VIDEO && (ext.den!=1 || ext.num!=1))
  99. continue;
  100. if (av_gcd(ext.den, ext.num) != 1)
  101. continue;
  102. if ( bestq.num==0
  103. || av_nearer_q(target, bestq, q) < 0
  104. || ext.num==1 && ext.den==1 && av_nearer_q(target, bestq, q) == 0) {
  105. bestq = q;
  106. s->frame_rate_index = i;
  107. s->mpeg2_frame_rate_ext.num = ext.num;
  108. s->mpeg2_frame_rate_ext.den = ext.den;
  109. }
  110. }
  111. }
  112. }
  113. if (av_cmp_q(target, bestq))
  114. return -1;
  115. else
  116. return 0;
  117. }
  118. static av_cold int encode_init(AVCodecContext *avctx)
  119. {
  120. MpegEncContext *s = avctx->priv_data;
  121. if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO && avctx->height > 2800)
  122. avctx->thread_count = 1;
  123. if (ff_MPV_encode_init(avctx) < 0)
  124. return -1;
  125. if (find_frame_rate_index(s) < 0) {
  126. if (s->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  127. av_log(avctx, AV_LOG_ERROR, "MPEG1/2 does not support %d/%d fps\n",
  128. avctx->time_base.den, avctx->time_base.num);
  129. return -1;
  130. } else {
  131. av_log(avctx, AV_LOG_INFO,
  132. "MPEG1/2 does not support %d/%d fps, there may be AV sync issues\n",
  133. avctx->time_base.den, avctx->time_base.num);
  134. }
  135. }
  136. if (avctx->profile == FF_PROFILE_UNKNOWN) {
  137. if (avctx->level != FF_LEVEL_UNKNOWN) {
  138. av_log(avctx, AV_LOG_ERROR, "Set profile and level\n");
  139. return -1;
  140. }
  141. /* Main or 4:2:2 */
  142. avctx->profile = s->chroma_format == CHROMA_420 ? 4 : 0;
  143. }
  144. if (avctx->level == FF_LEVEL_UNKNOWN) {
  145. if (avctx->profile == 0) { /* 4:2:2 */
  146. if (avctx->width <= 720 && avctx->height <= 608)
  147. avctx->level = 5; /* Main */
  148. else
  149. avctx->level = 2; /* High */
  150. } else {
  151. if (avctx->profile != 1 && s->chroma_format != CHROMA_420) {
  152. av_log(avctx, AV_LOG_ERROR,
  153. "Only High(1) and 4:2:2(0) profiles support 4:2:2 color sampling\n");
  154. return -1;
  155. }
  156. if (avctx->width <= 720 && avctx->height <= 576)
  157. avctx->level = 8; /* Main */
  158. else if (avctx->width <= 1440)
  159. avctx->level = 6; /* High 1440 */
  160. else
  161. avctx->level = 4; /* High */
  162. }
  163. }
  164. if ((avctx->width & 0xFFF) == 0 && (avctx->height & 0xFFF) == 1) {
  165. av_log(avctx, AV_LOG_ERROR, "Width / Height is invalid for MPEG2\n");
  166. return AVERROR(EINVAL);
  167. }
  168. if (s->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
  169. if ((avctx->width & 0xFFF) == 0 || (avctx->height & 0xFFF) == 0) {
  170. av_log(avctx, AV_LOG_ERROR, "Width or Height are not allowed to be multiplies of 4096\n"
  171. "add '-strict %d' if you want to use them anyway.\n", FF_COMPLIANCE_UNOFFICIAL);
  172. return AVERROR(EINVAL);
  173. }
  174. }
  175. s->drop_frame_timecode = s->drop_frame_timecode || !!(avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE);
  176. if (s->drop_frame_timecode)
  177. s->tc.flags |= AV_TIMECODE_FLAG_DROPFRAME;
  178. if (s->drop_frame_timecode && s->frame_rate_index != 4) {
  179. av_log(avctx, AV_LOG_ERROR,
  180. "Drop frame time code only allowed with 1001/30000 fps\n");
  181. return -1;
  182. }
  183. if (s->tc_opt_str) {
  184. AVRational rate = ff_mpeg12_frame_rate_tab[s->frame_rate_index];
  185. int ret = av_timecode_init_from_string(&s->tc, rate, s->tc_opt_str, s);
  186. if (ret < 0)
  187. return ret;
  188. s->drop_frame_timecode = !!(s->tc.flags & AV_TIMECODE_FLAG_DROPFRAME);
  189. s->avctx->timecode_frame_start = s->tc.start;
  190. } else {
  191. s->avctx->timecode_frame_start = 0; // default is -1
  192. }
  193. return 0;
  194. }
  195. static void put_header(MpegEncContext *s, int header)
  196. {
  197. avpriv_align_put_bits(&s->pb);
  198. put_bits(&s->pb, 16, header >> 16);
  199. put_sbits(&s->pb, 16, header);
  200. }
  201. /* put sequence header if needed */
  202. static void mpeg1_encode_sequence_header(MpegEncContext *s)
  203. {
  204. unsigned int vbv_buffer_size, fps, v;
  205. int i, constraint_parameter_flag;
  206. uint64_t time_code;
  207. float best_aspect_error = 1E10;
  208. float aspect_ratio = av_q2d(s->avctx->sample_aspect_ratio);
  209. if (aspect_ratio == 0.0)
  210. aspect_ratio = 1.0; // pixel aspect 1.1 (VGA)
  211. if (s->current_picture.f.key_frame) {
  212. AVRational framerate = ff_mpeg12_frame_rate_tab[s->frame_rate_index];
  213. /* mpeg1 header repeated every gop */
  214. put_header(s, SEQ_START_CODE);
  215. put_sbits(&s->pb, 12, s->width & 0xFFF);
  216. put_sbits(&s->pb, 12, s->height & 0xFFF);
  217. for (i = 1; i < 15; i++) {
  218. float error = aspect_ratio;
  219. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO || i <= 1)
  220. error -= 1.0 / ff_mpeg1_aspect[i];
  221. else
  222. error -= av_q2d(ff_mpeg2_aspect[i]) * s->height / s->width;
  223. error = FFABS(error);
  224. if (error < best_aspect_error) {
  225. best_aspect_error = error;
  226. s->aspect_ratio_info = i;
  227. }
  228. }
  229. put_bits(&s->pb, 4, s->aspect_ratio_info);
  230. put_bits(&s->pb, 4, s->frame_rate_index);
  231. if (s->avctx->rc_max_rate) {
  232. v = (s->avctx->rc_max_rate + 399) / 400;
  233. if (v > 0x3ffff && s->codec_id == AV_CODEC_ID_MPEG1VIDEO)
  234. v = 0x3ffff;
  235. } else {
  236. v = 0x3FFFF;
  237. }
  238. if (s->avctx->rc_buffer_size)
  239. vbv_buffer_size = s->avctx->rc_buffer_size;
  240. else
  241. /* VBV calculation: Scaled so that a VCD has the proper
  242. * VBV size of 40 kilobytes */
  243. vbv_buffer_size = ((20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024;
  244. vbv_buffer_size = (vbv_buffer_size + 16383) / 16384;
  245. put_sbits(&s->pb, 18, v);
  246. put_bits(&s->pb, 1, 1); // marker
  247. put_sbits(&s->pb, 10, vbv_buffer_size);
  248. constraint_parameter_flag =
  249. s->width <= 768 &&
  250. s->height <= 576 &&
  251. s->mb_width * s->mb_height <= 396 &&
  252. s->mb_width * s->mb_height * framerate.num <= 396 * 25 * framerate.den &&
  253. framerate.num <= framerate.den * 30 &&
  254. s->avctx->me_range &&
  255. s->avctx->me_range < 128 &&
  256. vbv_buffer_size <= 20 &&
  257. v <= 1856000 / 400 &&
  258. s->codec_id == AV_CODEC_ID_MPEG1VIDEO;
  259. put_bits(&s->pb, 1, constraint_parameter_flag);
  260. ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
  261. ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
  262. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  263. put_header(s, EXT_START_CODE);
  264. put_bits(&s->pb, 4, 1); // seq ext
  265. put_bits(&s->pb, 1, s->avctx->profile == 0); // escx 1 for 4:2:2 profile
  266. put_bits(&s->pb, 3, s->avctx->profile); // profile
  267. put_bits(&s->pb, 4, s->avctx->level); // level
  268. put_bits(&s->pb, 1, s->progressive_sequence);
  269. put_bits(&s->pb, 2, s->chroma_format);
  270. put_bits(&s->pb, 2, s->width >> 12);
  271. put_bits(&s->pb, 2, s->height >> 12);
  272. put_bits(&s->pb, 12, v >> 18); // bitrate ext
  273. put_bits(&s->pb, 1, 1); // marker
  274. put_bits(&s->pb, 8, vbv_buffer_size >> 10); // vbv buffer ext
  275. put_bits(&s->pb, 1, s->low_delay);
  276. put_bits(&s->pb, 2, s->mpeg2_frame_rate_ext.num-1); // frame_rate_ext_n
  277. put_bits(&s->pb, 5, s->mpeg2_frame_rate_ext.den-1); // frame_rate_ext_d
  278. }
  279. put_header(s, GOP_START_CODE);
  280. put_bits(&s->pb, 1, s->drop_frame_timecode); // drop frame flag
  281. /* time code: we must convert from the real frame rate to a
  282. * fake MPEG frame rate in case of low frame rate */
  283. fps = (framerate.num + framerate.den / 2) / framerate.den;
  284. time_code = s->current_picture_ptr->f.coded_picture_number +
  285. s->avctx->timecode_frame_start;
  286. s->gop_picture_number = s->current_picture_ptr->f.coded_picture_number;
  287. av_assert0(s->drop_frame_timecode == !!(s->tc.flags & AV_TIMECODE_FLAG_DROPFRAME));
  288. if (s->drop_frame_timecode)
  289. time_code = av_timecode_adjust_ntsc_framenum2(time_code, fps);
  290. put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24));
  291. put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60));
  292. put_bits(&s->pb, 1, 1);
  293. put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60));
  294. put_bits(&s->pb, 6, (uint32_t)((time_code % fps)));
  295. put_bits(&s->pb, 1, !!(s->flags & CODEC_FLAG_CLOSED_GOP));
  296. put_bits(&s->pb, 1, 0); // broken link
  297. }
  298. }
  299. static inline void encode_mb_skip_run(MpegEncContext *s, int run)
  300. {
  301. while (run >= 33) {
  302. put_bits(&s->pb, 11, 0x008);
  303. run -= 33;
  304. }
  305. put_bits(&s->pb, ff_mpeg12_mbAddrIncrTable[run][1],
  306. ff_mpeg12_mbAddrIncrTable[run][0]);
  307. }
  308. static av_always_inline void put_qscale(MpegEncContext *s)
  309. {
  310. if (s->q_scale_type) {
  311. av_assert2(s->qscale >= 1 && s->qscale <= 12);
  312. put_bits(&s->pb, 5, inv_non_linear_qscale[s->qscale]);
  313. } else {
  314. put_bits(&s->pb, 5, s->qscale);
  315. }
  316. }
  317. void ff_mpeg1_encode_slice_header(MpegEncContext *s)
  318. {
  319. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->height > 2800) {
  320. put_header(s, SLICE_MIN_START_CODE + (s->mb_y & 127));
  321. /* slice_vertical_position_extension */
  322. put_bits(&s->pb, 3, s->mb_y >> 7);
  323. } else {
  324. put_header(s, SLICE_MIN_START_CODE + s->mb_y);
  325. }
  326. put_qscale(s);
  327. /* slice extra information */
  328. put_bits(&s->pb, 1, 0);
  329. }
  330. void ff_mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
  331. {
  332. AVFrameSideData *side_data;
  333. mpeg1_encode_sequence_header(s);
  334. /* mpeg1 picture header */
  335. put_header(s, PICTURE_START_CODE);
  336. /* temporal reference */
  337. // RAL: s->picture_number instead of s->fake_picture_number
  338. put_bits(&s->pb, 10,
  339. (s->picture_number - s->gop_picture_number) & 0x3ff);
  340. put_bits(&s->pb, 3, s->pict_type);
  341. s->vbv_delay_ptr = s->pb.buf + put_bits_count(&s->pb) / 8;
  342. put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */
  343. // RAL: Forward f_code also needed for B-frames
  344. if (s->pict_type == AV_PICTURE_TYPE_P ||
  345. s->pict_type == AV_PICTURE_TYPE_B) {
  346. put_bits(&s->pb, 1, 0); /* half pel coordinates */
  347. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO)
  348. put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
  349. else
  350. put_bits(&s->pb, 3, 7); /* forward_f_code */
  351. }
  352. // RAL: Backward f_code necessary for B-frames
  353. if (s->pict_type == AV_PICTURE_TYPE_B) {
  354. put_bits(&s->pb, 1, 0); /* half pel coordinates */
  355. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO)
  356. put_bits(&s->pb, 3, s->b_code); /* backward_f_code */
  357. else
  358. put_bits(&s->pb, 3, 7); /* backward_f_code */
  359. }
  360. put_bits(&s->pb, 1, 0); /* extra bit picture */
  361. s->frame_pred_frame_dct = 1;
  362. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  363. put_header(s, EXT_START_CODE);
  364. put_bits(&s->pb, 4, 8); /* pic ext */
  365. if (s->pict_type == AV_PICTURE_TYPE_P ||
  366. s->pict_type == AV_PICTURE_TYPE_B) {
  367. put_bits(&s->pb, 4, s->f_code);
  368. put_bits(&s->pb, 4, s->f_code);
  369. } else {
  370. put_bits(&s->pb, 8, 255);
  371. }
  372. if (s->pict_type == AV_PICTURE_TYPE_B) {
  373. put_bits(&s->pb, 4, s->b_code);
  374. put_bits(&s->pb, 4, s->b_code);
  375. } else {
  376. put_bits(&s->pb, 8, 255);
  377. }
  378. put_bits(&s->pb, 2, s->intra_dc_precision);
  379. av_assert0(s->picture_structure == PICT_FRAME);
  380. put_bits(&s->pb, 2, s->picture_structure);
  381. if (s->progressive_sequence)
  382. put_bits(&s->pb, 1, 0); /* no repeat */
  383. else
  384. put_bits(&s->pb, 1, s->current_picture_ptr->f.top_field_first);
  385. /* XXX: optimize the generation of this flag with entropy measures */
  386. s->frame_pred_frame_dct = s->progressive_sequence;
  387. put_bits(&s->pb, 1, s->frame_pred_frame_dct);
  388. put_bits(&s->pb, 1, s->concealment_motion_vectors);
  389. put_bits(&s->pb, 1, s->q_scale_type);
  390. put_bits(&s->pb, 1, s->intra_vlc_format);
  391. put_bits(&s->pb, 1, s->alternate_scan);
  392. put_bits(&s->pb, 1, s->repeat_first_field);
  393. s->progressive_frame = s->progressive_sequence;
  394. /* chroma_420_type */
  395. put_bits(&s->pb, 1, s->chroma_format ==
  396. CHROMA_420 ? s->progressive_frame : 0);
  397. put_bits(&s->pb, 1, s->progressive_frame);
  398. put_bits(&s->pb, 1, 0); /* composite_display_flag */
  399. }
  400. if (s->scan_offset) {
  401. int i;
  402. put_header(s, USER_START_CODE);
  403. for (i = 0; i < sizeof(svcd_scan_offset_placeholder); i++)
  404. put_bits(&s->pb, 8, svcd_scan_offset_placeholder[i]);
  405. }
  406. side_data = av_frame_get_side_data(&s->current_picture_ptr->f,
  407. AV_FRAME_DATA_STEREO3D);
  408. if (side_data) {
  409. AVStereo3D *stereo = (AVStereo3D *)side_data->data;
  410. uint8_t fpa_type;
  411. switch (stereo->type) {
  412. case AV_STEREO3D_SIDEBYSIDE:
  413. fpa_type = 0x03;
  414. break;
  415. case AV_STEREO3D_TOPBOTTOM:
  416. fpa_type = 0x04;
  417. break;
  418. case AV_STEREO3D_2D:
  419. fpa_type = 0x08;
  420. break;
  421. case AV_STEREO3D_SIDEBYSIDE_QUINCUNX:
  422. fpa_type = 0x23;
  423. break;
  424. default:
  425. fpa_type = 0;
  426. break;
  427. }
  428. if (fpa_type != 0) {
  429. put_header(s, USER_START_CODE);
  430. put_bits(&s->pb, 8, 'J'); // S3D_video_format_signaling_identifier
  431. put_bits(&s->pb, 8, 'P');
  432. put_bits(&s->pb, 8, '3');
  433. put_bits(&s->pb, 8, 'D');
  434. put_bits(&s->pb, 8, 0x03); // S3D_video_format_length
  435. put_bits(&s->pb, 1, 1); // reserved_bit
  436. put_bits(&s->pb, 7, fpa_type); // S3D_video_format_type
  437. put_bits(&s->pb, 8, 0x04); // reserved_data[0]
  438. put_bits(&s->pb, 8, 0xFF); // reserved_data[1]
  439. }
  440. }
  441. s->mb_y = 0;
  442. ff_mpeg1_encode_slice_header(s);
  443. }
  444. static inline void put_mb_modes(MpegEncContext *s, int n, int bits,
  445. int has_mv, int field_motion)
  446. {
  447. put_bits(&s->pb, n, bits);
  448. if (!s->frame_pred_frame_dct) {
  449. if (has_mv)
  450. /* motion_type: frame/field */
  451. put_bits(&s->pb, 2, 2 - field_motion);
  452. put_bits(&s->pb, 1, s->interlaced_dct);
  453. }
  454. }
  455. // RAL: Parameter added: f_or_b_code
  456. static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
  457. {
  458. if (val == 0) {
  459. /* zero vector */
  460. put_bits(&s->pb,
  461. ff_mpeg12_mbMotionVectorTable[0][1],
  462. ff_mpeg12_mbMotionVectorTable[0][0]);
  463. } else {
  464. int code, sign, bits;
  465. int bit_size = f_or_b_code - 1;
  466. int range = 1 << bit_size;
  467. /* modulo encoding */
  468. val = sign_extend(val, 5 + bit_size);
  469. if (val >= 0) {
  470. val--;
  471. code = (val >> bit_size) + 1;
  472. bits = val & (range - 1);
  473. sign = 0;
  474. } else {
  475. val = -val;
  476. val--;
  477. code = (val >> bit_size) + 1;
  478. bits = val & (range - 1);
  479. sign = 1;
  480. }
  481. av_assert2(code > 0 && code <= 16);
  482. put_bits(&s->pb,
  483. ff_mpeg12_mbMotionVectorTable[code][1],
  484. ff_mpeg12_mbMotionVectorTable[code][0]);
  485. put_bits(&s->pb, 1, sign);
  486. if (bit_size > 0)
  487. put_bits(&s->pb, bit_size, bits);
  488. }
  489. }
  490. static inline void encode_dc(MpegEncContext *s, int diff, int component)
  491. {
  492. if (((unsigned) (diff + 255)) >= 511) {
  493. int index;
  494. if (diff < 0) {
  495. index = av_log2_16bit(-2 * diff);
  496. diff--;
  497. } else {
  498. index = av_log2_16bit(2 * diff);
  499. }
  500. if (component == 0)
  501. put_bits(&s->pb,
  502. ff_mpeg12_vlc_dc_lum_bits[index] + index,
  503. (ff_mpeg12_vlc_dc_lum_code[index] << index) +
  504. (diff & ((1 << index) - 1)));
  505. else
  506. put_bits(&s->pb,
  507. ff_mpeg12_vlc_dc_chroma_bits[index] + index,
  508. (ff_mpeg12_vlc_dc_chroma_code[index] << index) +
  509. (diff & ((1 << index) - 1)));
  510. } else {
  511. if (component == 0)
  512. put_bits(&s->pb,
  513. mpeg1_lum_dc_uni[diff + 255] & 0xFF,
  514. mpeg1_lum_dc_uni[diff + 255] >> 8);
  515. else
  516. put_bits(&s->pb,
  517. mpeg1_chr_dc_uni[diff + 255] & 0xFF,
  518. mpeg1_chr_dc_uni[diff + 255] >> 8);
  519. }
  520. }
  521. static void mpeg1_encode_block(MpegEncContext *s, int16_t *block, int n)
  522. {
  523. int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
  524. int code, component;
  525. const uint16_t (*table_vlc)[2] = ff_rl_mpeg1.table_vlc;
  526. last_index = s->block_last_index[n];
  527. /* DC coef */
  528. if (s->mb_intra) {
  529. component = (n <= 3 ? 0 : (n & 1) + 1);
  530. dc = block[0]; /* overflow is impossible */
  531. diff = dc - s->last_dc[component];
  532. encode_dc(s, diff, component);
  533. s->last_dc[component] = dc;
  534. i = 1;
  535. if (s->intra_vlc_format)
  536. table_vlc = ff_rl_mpeg2.table_vlc;
  537. } else {
  538. /* encode the first coefficient: needs to be done here because
  539. * it is handled slightly differently */
  540. level = block[0];
  541. if (abs(level) == 1) {
  542. code = ((uint32_t)level >> 31); /* the sign bit */
  543. put_bits(&s->pb, 2, code | 0x02);
  544. i = 1;
  545. } else {
  546. i = 0;
  547. last_non_zero = -1;
  548. goto next_coef;
  549. }
  550. }
  551. /* now quantify & encode AC coefs */
  552. last_non_zero = i - 1;
  553. for (; i <= last_index; i++) {
  554. j = s->intra_scantable.permutated[i];
  555. level = block[j];
  556. next_coef:
  557. /* encode using VLC */
  558. if (level != 0) {
  559. run = i - last_non_zero - 1;
  560. alevel = level;
  561. MASK_ABS(sign, alevel);
  562. sign &= 1;
  563. if (alevel <= mpeg1_max_level[0][run]) {
  564. code = mpeg1_index_run[0][run] + alevel - 1;
  565. /* store the VLC & sign at once */
  566. put_bits(&s->pb, table_vlc[code][1] + 1,
  567. (table_vlc[code][0] << 1) + sign);
  568. } else {
  569. /* escape seems to be pretty rare <5% so I do not optimize it */
  570. put_bits(&s->pb, table_vlc[111][1], table_vlc[111][0]);
  571. /* escape: only clip in this case */
  572. put_bits(&s->pb, 6, run);
  573. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
  574. if (alevel < 128) {
  575. put_sbits(&s->pb, 8, level);
  576. } else {
  577. if (level < 0)
  578. put_bits(&s->pb, 16, 0x8001 + level + 255);
  579. else
  580. put_sbits(&s->pb, 16, level);
  581. }
  582. } else {
  583. put_sbits(&s->pb, 12, level);
  584. }
  585. }
  586. last_non_zero = i;
  587. }
  588. }
  589. /* end of block */
  590. put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
  591. }
  592. static av_always_inline void mpeg1_encode_mb_internal(MpegEncContext *s,
  593. int16_t block[6][64],
  594. int motion_x, int motion_y,
  595. int mb_block_count)
  596. {
  597. int i, cbp;
  598. const int mb_x = s->mb_x;
  599. const int mb_y = s->mb_y;
  600. const int first_mb = mb_x == s->resync_mb_x && mb_y == s->resync_mb_y;
  601. /* compute cbp */
  602. cbp = 0;
  603. for (i = 0; i < mb_block_count; i++)
  604. if (s->block_last_index[i] >= 0)
  605. cbp |= 1 << (mb_block_count - 1 - i);
  606. if (cbp == 0 && !first_mb && s->mv_type == MV_TYPE_16X16 &&
  607. (mb_x != s->mb_width - 1 ||
  608. (mb_y != s->end_mb_y - 1 && s->codec_id == AV_CODEC_ID_MPEG1VIDEO)) &&
  609. ((s->pict_type == AV_PICTURE_TYPE_P && (motion_x | motion_y) == 0) ||
  610. (s->pict_type == AV_PICTURE_TYPE_B && s->mv_dir == s->last_mv_dir &&
  611. (((s->mv_dir & MV_DIR_FORWARD)
  612. ? ((s->mv[0][0][0] - s->last_mv[0][0][0]) |
  613. (s->mv[0][0][1] - s->last_mv[0][0][1])) : 0) |
  614. ((s->mv_dir & MV_DIR_BACKWARD)
  615. ? ((s->mv[1][0][0] - s->last_mv[1][0][0]) |
  616. (s->mv[1][0][1] - s->last_mv[1][0][1])) : 0)) == 0))) {
  617. s->mb_skip_run++;
  618. s->qscale -= s->dquant;
  619. s->skip_count++;
  620. s->misc_bits++;
  621. s->last_bits++;
  622. if (s->pict_type == AV_PICTURE_TYPE_P) {
  623. s->last_mv[0][0][0] =
  624. s->last_mv[0][0][1] =
  625. s->last_mv[0][1][0] =
  626. s->last_mv[0][1][1] = 0;
  627. }
  628. } else {
  629. if (first_mb) {
  630. av_assert0(s->mb_skip_run == 0);
  631. encode_mb_skip_run(s, s->mb_x);
  632. } else {
  633. encode_mb_skip_run(s, s->mb_skip_run);
  634. }
  635. if (s->pict_type == AV_PICTURE_TYPE_I) {
  636. if (s->dquant && cbp) {
  637. /* macroblock_type: macroblock_quant = 1 */
  638. put_mb_modes(s, 2, 1, 0, 0);
  639. put_qscale(s);
  640. } else {
  641. /* macroblock_type: macroblock_quant = 0 */
  642. put_mb_modes(s, 1, 1, 0, 0);
  643. s->qscale -= s->dquant;
  644. }
  645. s->misc_bits += get_bits_diff(s);
  646. s->i_count++;
  647. } else if (s->mb_intra) {
  648. if (s->dquant && cbp) {
  649. put_mb_modes(s, 6, 0x01, 0, 0);
  650. put_qscale(s);
  651. } else {
  652. put_mb_modes(s, 5, 0x03, 0, 0);
  653. s->qscale -= s->dquant;
  654. }
  655. s->misc_bits += get_bits_diff(s);
  656. s->i_count++;
  657. memset(s->last_mv, 0, sizeof(s->last_mv));
  658. } else if (s->pict_type == AV_PICTURE_TYPE_P) {
  659. if (s->mv_type == MV_TYPE_16X16) {
  660. if (cbp != 0) {
  661. if ((motion_x | motion_y) == 0) {
  662. if (s->dquant) {
  663. /* macroblock_pattern & quant */
  664. put_mb_modes(s, 5, 1, 0, 0);
  665. put_qscale(s);
  666. } else {
  667. /* macroblock_pattern only */
  668. put_mb_modes(s, 2, 1, 0, 0);
  669. }
  670. s->misc_bits += get_bits_diff(s);
  671. } else {
  672. if (s->dquant) {
  673. put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */
  674. put_qscale(s);
  675. } else {
  676. put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */
  677. }
  678. s->misc_bits += get_bits_diff(s);
  679. // RAL: f_code parameter added
  680. mpeg1_encode_motion(s,
  681. motion_x - s->last_mv[0][0][0],
  682. s->f_code);
  683. // RAL: f_code parameter added
  684. mpeg1_encode_motion(s,
  685. motion_y - s->last_mv[0][0][1],
  686. s->f_code);
  687. s->mv_bits += get_bits_diff(s);
  688. }
  689. } else {
  690. put_bits(&s->pb, 3, 1); /* motion only */
  691. if (!s->frame_pred_frame_dct)
  692. put_bits(&s->pb, 2, 2); /* motion_type: frame */
  693. s->misc_bits += get_bits_diff(s);
  694. // RAL: f_code parameter added
  695. mpeg1_encode_motion(s,
  696. motion_x - s->last_mv[0][0][0],
  697. s->f_code);
  698. // RAL: f_code parameter added
  699. mpeg1_encode_motion(s,
  700. motion_y - s->last_mv[0][0][1],
  701. s->f_code);
  702. s->qscale -= s->dquant;
  703. s->mv_bits += get_bits_diff(s);
  704. }
  705. s->last_mv[0][1][0] = s->last_mv[0][0][0] = motion_x;
  706. s->last_mv[0][1][1] = s->last_mv[0][0][1] = motion_y;
  707. } else {
  708. av_assert2(!s->frame_pred_frame_dct && s->mv_type == MV_TYPE_FIELD);
  709. if (cbp) {
  710. if (s->dquant) {
  711. put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */
  712. put_qscale(s);
  713. } else {
  714. put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */
  715. }
  716. } else {
  717. put_bits(&s->pb, 3, 1); /* motion only */
  718. put_bits(&s->pb, 2, 1); /* motion_type: field */
  719. s->qscale -= s->dquant;
  720. }
  721. s->misc_bits += get_bits_diff(s);
  722. for (i = 0; i < 2; i++) {
  723. put_bits(&s->pb, 1, s->field_select[0][i]);
  724. mpeg1_encode_motion(s,
  725. s->mv[0][i][0] - s->last_mv[0][i][0],
  726. s->f_code);
  727. mpeg1_encode_motion(s,
  728. s->mv[0][i][1] - (s->last_mv[0][i][1] >> 1),
  729. s->f_code);
  730. s->last_mv[0][i][0] = s->mv[0][i][0];
  731. s->last_mv[0][i][1] = 2 * s->mv[0][i][1];
  732. }
  733. s->mv_bits += get_bits_diff(s);
  734. }
  735. if (cbp) {
  736. if (s->chroma_y_shift) {
  737. put_bits(&s->pb,
  738. ff_mpeg12_mbPatTable[cbp][1],
  739. ff_mpeg12_mbPatTable[cbp][0]);
  740. } else {
  741. put_bits(&s->pb,
  742. ff_mpeg12_mbPatTable[cbp >> 2][1],
  743. ff_mpeg12_mbPatTable[cbp >> 2][0]);
  744. put_sbits(&s->pb, 2, cbp);
  745. }
  746. }
  747. s->f_count++;
  748. } else {
  749. if (s->mv_type == MV_TYPE_16X16) {
  750. if (cbp) { // With coded bloc pattern
  751. if (s->dquant) {
  752. if (s->mv_dir == MV_DIR_FORWARD)
  753. put_mb_modes(s, 6, 3, 1, 0);
  754. else
  755. put_mb_modes(s, 8 - s->mv_dir, 2, 1, 0);
  756. put_qscale(s);
  757. } else {
  758. put_mb_modes(s, 5 - s->mv_dir, 3, 1, 0);
  759. }
  760. } else { // No coded bloc pattern
  761. put_bits(&s->pb, 5 - s->mv_dir, 2);
  762. if (!s->frame_pred_frame_dct)
  763. put_bits(&s->pb, 2, 2); /* motion_type: frame */
  764. s->qscale -= s->dquant;
  765. }
  766. s->misc_bits += get_bits_diff(s);
  767. if (s->mv_dir & MV_DIR_FORWARD) {
  768. mpeg1_encode_motion(s,
  769. s->mv[0][0][0] - s->last_mv[0][0][0],
  770. s->f_code);
  771. mpeg1_encode_motion(s,
  772. s->mv[0][0][1] - s->last_mv[0][0][1],
  773. s->f_code);
  774. s->last_mv[0][0][0] =
  775. s->last_mv[0][1][0] = s->mv[0][0][0];
  776. s->last_mv[0][0][1] =
  777. s->last_mv[0][1][1] = s->mv[0][0][1];
  778. s->f_count++;
  779. }
  780. if (s->mv_dir & MV_DIR_BACKWARD) {
  781. mpeg1_encode_motion(s,
  782. s->mv[1][0][0] - s->last_mv[1][0][0],
  783. s->b_code);
  784. mpeg1_encode_motion(s,
  785. s->mv[1][0][1] - s->last_mv[1][0][1],
  786. s->b_code);
  787. s->last_mv[1][0][0] =
  788. s->last_mv[1][1][0] = s->mv[1][0][0];
  789. s->last_mv[1][0][1] =
  790. s->last_mv[1][1][1] = s->mv[1][0][1];
  791. s->b_count++;
  792. }
  793. } else {
  794. av_assert2(s->mv_type == MV_TYPE_FIELD);
  795. av_assert2(!s->frame_pred_frame_dct);
  796. if (cbp) { // With coded bloc pattern
  797. if (s->dquant) {
  798. if (s->mv_dir == MV_DIR_FORWARD)
  799. put_mb_modes(s, 6, 3, 1, 1);
  800. else
  801. put_mb_modes(s, 8 - s->mv_dir, 2, 1, 1);
  802. put_qscale(s);
  803. } else {
  804. put_mb_modes(s, 5 - s->mv_dir, 3, 1, 1);
  805. }
  806. } else { // No coded bloc pattern
  807. put_bits(&s->pb, 5 - s->mv_dir, 2);
  808. put_bits(&s->pb, 2, 1); /* motion_type: field */
  809. s->qscale -= s->dquant;
  810. }
  811. s->misc_bits += get_bits_diff(s);
  812. if (s->mv_dir & MV_DIR_FORWARD) {
  813. for (i = 0; i < 2; i++) {
  814. put_bits(&s->pb, 1, s->field_select[0][i]);
  815. mpeg1_encode_motion(s,
  816. s->mv[0][i][0] - s->last_mv[0][i][0],
  817. s->f_code);
  818. mpeg1_encode_motion(s,
  819. s->mv[0][i][1] - (s->last_mv[0][i][1] >> 1),
  820. s->f_code);
  821. s->last_mv[0][i][0] = s->mv[0][i][0];
  822. s->last_mv[0][i][1] = s->mv[0][i][1] * 2;
  823. }
  824. s->f_count++;
  825. }
  826. if (s->mv_dir & MV_DIR_BACKWARD) {
  827. for (i = 0; i < 2; i++) {
  828. put_bits(&s->pb, 1, s->field_select[1][i]);
  829. mpeg1_encode_motion(s,
  830. s->mv[1][i][0] - s->last_mv[1][i][0],
  831. s->b_code);
  832. mpeg1_encode_motion(s,
  833. s->mv[1][i][1] - (s->last_mv[1][i][1] >> 1),
  834. s->b_code);
  835. s->last_mv[1][i][0] = s->mv[1][i][0];
  836. s->last_mv[1][i][1] = s->mv[1][i][1] * 2;
  837. }
  838. s->b_count++;
  839. }
  840. }
  841. s->mv_bits += get_bits_diff(s);
  842. if (cbp) {
  843. if (s->chroma_y_shift) {
  844. put_bits(&s->pb,
  845. ff_mpeg12_mbPatTable[cbp][1],
  846. ff_mpeg12_mbPatTable[cbp][0]);
  847. } else {
  848. put_bits(&s->pb,
  849. ff_mpeg12_mbPatTable[cbp >> 2][1],
  850. ff_mpeg12_mbPatTable[cbp >> 2][0]);
  851. put_sbits(&s->pb, 2, cbp);
  852. }
  853. }
  854. }
  855. for (i = 0; i < mb_block_count; i++)
  856. if (cbp & (1 << (mb_block_count - 1 - i)))
  857. mpeg1_encode_block(s, block[i], i);
  858. s->mb_skip_run = 0;
  859. if (s->mb_intra)
  860. s->i_tex_bits += get_bits_diff(s);
  861. else
  862. s->p_tex_bits += get_bits_diff(s);
  863. }
  864. }
  865. void ff_mpeg1_encode_mb(MpegEncContext *s, int16_t block[6][64],
  866. int motion_x, int motion_y)
  867. {
  868. if (s->chroma_format == CHROMA_420)
  869. mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 6);
  870. else
  871. mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8);
  872. }
  873. av_cold void ff_mpeg1_encode_init(MpegEncContext *s)
  874. {
  875. static int done = 0;
  876. ff_mpeg12_common_init(s);
  877. if (!done) {
  878. int f_code;
  879. int mv;
  880. int i;
  881. done = 1;
  882. ff_init_rl(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
  883. ff_init_rl(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]);
  884. for (i = 0; i < 64; i++) {
  885. mpeg1_max_level[0][i] = ff_rl_mpeg1.max_level[0][i];
  886. mpeg1_index_run[0][i] = ff_rl_mpeg1.index_run[0][i];
  887. }
  888. init_uni_ac_vlc(&ff_rl_mpeg1, uni_mpeg1_ac_vlc_len);
  889. if (s->intra_vlc_format)
  890. init_uni_ac_vlc(&ff_rl_mpeg2, uni_mpeg2_ac_vlc_len);
  891. /* build unified dc encoding tables */
  892. for (i = -255; i < 256; i++) {
  893. int adiff, index;
  894. int bits, code;
  895. int diff = i;
  896. adiff = FFABS(diff);
  897. if (diff < 0)
  898. diff--;
  899. index = av_log2(2 * adiff);
  900. bits = ff_mpeg12_vlc_dc_lum_bits[index] + index;
  901. code = (ff_mpeg12_vlc_dc_lum_code[index] << index) +
  902. (diff & ((1 << index) - 1));
  903. mpeg1_lum_dc_uni[i + 255] = bits + (code << 8);
  904. bits = ff_mpeg12_vlc_dc_chroma_bits[index] + index;
  905. code = (ff_mpeg12_vlc_dc_chroma_code[index] << index) +
  906. (diff & ((1 << index) - 1));
  907. mpeg1_chr_dc_uni[i + 255] = bits + (code << 8);
  908. }
  909. for (f_code = 1; f_code <= MAX_FCODE; f_code++)
  910. for (mv = -MAX_MV; mv <= MAX_MV; mv++) {
  911. int len;
  912. if (mv == 0) {
  913. len = ff_mpeg12_mbMotionVectorTable[0][1];
  914. } else {
  915. int val, bit_size, code;
  916. bit_size = f_code - 1;
  917. val = mv;
  918. if (val < 0)
  919. val = -val;
  920. val--;
  921. code = (val >> bit_size) + 1;
  922. if (code < 17)
  923. len = ff_mpeg12_mbMotionVectorTable[code][1] +
  924. 1 + bit_size;
  925. else
  926. len = ff_mpeg12_mbMotionVectorTable[16][1] +
  927. 2 + bit_size;
  928. }
  929. mv_penalty[f_code][mv + MAX_MV] = len;
  930. }
  931. for (f_code = MAX_FCODE; f_code > 0; f_code--)
  932. for (mv = -(8 << f_code); mv < (8 << f_code); mv++)
  933. fcode_tab[mv + MAX_MV] = f_code;
  934. }
  935. s->me.mv_penalty = mv_penalty;
  936. s->fcode_tab = fcode_tab;
  937. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
  938. s->min_qcoeff = -255;
  939. s->max_qcoeff = 255;
  940. } else {
  941. s->min_qcoeff = -2047;
  942. s->max_qcoeff = 2047;
  943. }
  944. if (s->intra_vlc_format) {
  945. s->intra_ac_vlc_length =
  946. s->intra_ac_vlc_last_length = uni_mpeg2_ac_vlc_len;
  947. } else {
  948. s->intra_ac_vlc_length =
  949. s->intra_ac_vlc_last_length = uni_mpeg1_ac_vlc_len;
  950. }
  951. s->inter_ac_vlc_length =
  952. s->inter_ac_vlc_last_length = uni_mpeg1_ac_vlc_len;
  953. }
  954. #define OFFSET(x) offsetof(MpegEncContext, x)
  955. #define VE AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
  956. #define COMMON_OPTS \
  957. { "gop_timecode", "MPEG GOP Timecode in hh:mm:ss[:;.]ff format", \
  958. OFFSET(tc_opt_str), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, VE },\
  959. { "intra_vlc", "Use MPEG-2 intra VLC table.", \
  960. OFFSET(intra_vlc_format), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE }, \
  961. { "drop_frame_timecode", "Timecode is in drop frame format.", \
  962. OFFSET(drop_frame_timecode), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE }, \
  963. { "scan_offset", "Reserve space for SVCD scan offset user data.", \
  964. OFFSET(scan_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
  965. static const AVOption mpeg1_options[] = {
  966. COMMON_OPTS
  967. FF_MPV_COMMON_OPTS
  968. { NULL },
  969. };
  970. static const AVOption mpeg2_options[] = {
  971. COMMON_OPTS
  972. { "non_linear_quant", "Use nonlinear quantizer.", OFFSET(q_scale_type), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
  973. { "alternate_scan", "Enable alternate scantable.", OFFSET(alternate_scan), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
  974. FF_MPV_COMMON_OPTS
  975. { NULL },
  976. };
  977. #define mpeg12_class(x) \
  978. static const AVClass mpeg ## x ## _class = { \
  979. .class_name = "mpeg" # x "video encoder", \
  980. .item_name = av_default_item_name, \
  981. .option = mpeg ## x ## _options, \
  982. .version = LIBAVUTIL_VERSION_INT, \
  983. };
  984. mpeg12_class(1)
  985. mpeg12_class(2)
  986. AVCodec ff_mpeg1video_encoder = {
  987. .name = "mpeg1video",
  988. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
  989. .type = AVMEDIA_TYPE_VIDEO,
  990. .id = AV_CODEC_ID_MPEG1VIDEO,
  991. .priv_data_size = sizeof(MpegEncContext),
  992. .init = encode_init,
  993. .encode2 = ff_MPV_encode_picture,
  994. .close = ff_MPV_encode_end,
  995. .supported_framerates = ff_mpeg12_frame_rate_tab + 1,
  996. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
  997. AV_PIX_FMT_NONE },
  998. .capabilities = CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
  999. .priv_class = &mpeg1_class,
  1000. };
  1001. AVCodec ff_mpeg2video_encoder = {
  1002. .name = "mpeg2video",
  1003. .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
  1004. .type = AVMEDIA_TYPE_VIDEO,
  1005. .id = AV_CODEC_ID_MPEG2VIDEO,
  1006. .priv_data_size = sizeof(MpegEncContext),
  1007. .init = encode_init,
  1008. .encode2 = ff_MPV_encode_picture,
  1009. .close = ff_MPV_encode_end,
  1010. .supported_framerates = ff_mpeg2_frame_rate_tab,
  1011. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
  1012. AV_PIX_FMT_YUV422P,
  1013. AV_PIX_FMT_NONE },
  1014. .capabilities = CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
  1015. .priv_class = &mpeg2_class,
  1016. };