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.

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