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.

1177 lines
45KB

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