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.

1165 lines
44KB

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