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.

1178 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 multiples 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->avctx->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. unsigned int diff_u = diff + 255;
  524. if (diff_u >= 511) {
  525. int index;
  526. if (diff < 0) {
  527. index = av_log2_16bit(-2 * diff);
  528. diff--;
  529. } else {
  530. index = av_log2_16bit(2 * diff);
  531. }
  532. if (component == 0)
  533. put_bits(&s->pb,
  534. ff_mpeg12_vlc_dc_lum_bits[index] + index,
  535. (ff_mpeg12_vlc_dc_lum_code[index] << index) +
  536. av_mod_uintp2(diff, index));
  537. else
  538. put_bits(&s->pb,
  539. ff_mpeg12_vlc_dc_chroma_bits[index] + index,
  540. (ff_mpeg12_vlc_dc_chroma_code[index] << index) +
  541. av_mod_uintp2(diff, index));
  542. } else {
  543. if (component == 0)
  544. put_bits(&s->pb,
  545. mpeg1_lum_dc_uni[diff + 255] & 0xFF,
  546. mpeg1_lum_dc_uni[diff + 255] >> 8);
  547. else
  548. put_bits(&s->pb,
  549. mpeg1_chr_dc_uni[diff + 255] & 0xFF,
  550. mpeg1_chr_dc_uni[diff + 255] >> 8);
  551. }
  552. }
  553. static void mpeg1_encode_block(MpegEncContext *s, int16_t *block, int n)
  554. {
  555. int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
  556. int code, component;
  557. const uint16_t (*table_vlc)[2] = ff_rl_mpeg1.table_vlc;
  558. last_index = s->block_last_index[n];
  559. /* DC coef */
  560. if (s->mb_intra) {
  561. component = (n <= 3 ? 0 : (n & 1) + 1);
  562. dc = block[0]; /* overflow is impossible */
  563. diff = dc - s->last_dc[component];
  564. encode_dc(s, diff, component);
  565. s->last_dc[component] = dc;
  566. i = 1;
  567. if (s->intra_vlc_format)
  568. table_vlc = ff_rl_mpeg2.table_vlc;
  569. } else {
  570. /* encode the first coefficient: needs to be done here because
  571. * it is handled slightly differently */
  572. level = block[0];
  573. if (abs(level) == 1) {
  574. code = ((uint32_t)level >> 31); /* the sign bit */
  575. put_bits(&s->pb, 2, code | 0x02);
  576. i = 1;
  577. } else {
  578. i = 0;
  579. last_non_zero = -1;
  580. goto next_coef;
  581. }
  582. }
  583. /* now quantify & encode AC coefs */
  584. last_non_zero = i - 1;
  585. for (; i <= last_index; i++) {
  586. j = s->intra_scantable.permutated[i];
  587. level = block[j];
  588. next_coef:
  589. /* encode using VLC */
  590. if (level != 0) {
  591. run = i - last_non_zero - 1;
  592. alevel = level;
  593. MASK_ABS(sign, alevel);
  594. sign &= 1;
  595. if (alevel <= mpeg1_max_level[0][run]) {
  596. code = mpeg1_index_run[0][run] + alevel - 1;
  597. /* store the VLC & sign at once */
  598. put_bits(&s->pb, table_vlc[code][1] + 1,
  599. (table_vlc[code][0] << 1) + sign);
  600. } else {
  601. /* escape seems to be pretty rare <5% so I do not optimize it */
  602. put_bits(&s->pb, table_vlc[111][1], table_vlc[111][0]);
  603. /* escape: only clip in this case */
  604. put_bits(&s->pb, 6, run);
  605. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
  606. if (alevel < 128) {
  607. put_sbits(&s->pb, 8, level);
  608. } else {
  609. if (level < 0)
  610. put_bits(&s->pb, 16, 0x8001 + level + 255);
  611. else
  612. put_sbits(&s->pb, 16, level);
  613. }
  614. } else {
  615. put_sbits(&s->pb, 12, level);
  616. }
  617. }
  618. last_non_zero = i;
  619. }
  620. }
  621. /* end of block */
  622. put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
  623. }
  624. static av_always_inline void mpeg1_encode_mb_internal(MpegEncContext *s,
  625. int16_t block[8][64],
  626. int motion_x, int motion_y,
  627. int mb_block_count)
  628. {
  629. int i, cbp;
  630. const int mb_x = s->mb_x;
  631. const int mb_y = s->mb_y;
  632. const int first_mb = mb_x == s->resync_mb_x && mb_y == s->resync_mb_y;
  633. /* compute cbp */
  634. cbp = 0;
  635. for (i = 0; i < mb_block_count; i++)
  636. if (s->block_last_index[i] >= 0)
  637. cbp |= 1 << (mb_block_count - 1 - i);
  638. if (cbp == 0 && !first_mb && s->mv_type == MV_TYPE_16X16 &&
  639. (mb_x != s->mb_width - 1 ||
  640. (mb_y != s->end_mb_y - 1 && s->codec_id == AV_CODEC_ID_MPEG1VIDEO)) &&
  641. ((s->pict_type == AV_PICTURE_TYPE_P && (motion_x | motion_y) == 0) ||
  642. (s->pict_type == AV_PICTURE_TYPE_B && s->mv_dir == s->last_mv_dir &&
  643. (((s->mv_dir & MV_DIR_FORWARD)
  644. ? ((s->mv[0][0][0] - s->last_mv[0][0][0]) |
  645. (s->mv[0][0][1] - s->last_mv[0][0][1])) : 0) |
  646. ((s->mv_dir & MV_DIR_BACKWARD)
  647. ? ((s->mv[1][0][0] - s->last_mv[1][0][0]) |
  648. (s->mv[1][0][1] - s->last_mv[1][0][1])) : 0)) == 0))) {
  649. s->mb_skip_run++;
  650. s->qscale -= s->dquant;
  651. s->skip_count++;
  652. s->misc_bits++;
  653. s->last_bits++;
  654. if (s->pict_type == AV_PICTURE_TYPE_P) {
  655. s->last_mv[0][0][0] =
  656. s->last_mv[0][0][1] =
  657. s->last_mv[0][1][0] =
  658. s->last_mv[0][1][1] = 0;
  659. }
  660. } else {
  661. if (first_mb) {
  662. av_assert0(s->mb_skip_run == 0);
  663. encode_mb_skip_run(s, s->mb_x);
  664. } else {
  665. encode_mb_skip_run(s, s->mb_skip_run);
  666. }
  667. if (s->pict_type == AV_PICTURE_TYPE_I) {
  668. if (s->dquant && cbp) {
  669. /* macroblock_type: macroblock_quant = 1 */
  670. put_mb_modes(s, 2, 1, 0, 0);
  671. put_qscale(s);
  672. } else {
  673. /* macroblock_type: macroblock_quant = 0 */
  674. put_mb_modes(s, 1, 1, 0, 0);
  675. s->qscale -= s->dquant;
  676. }
  677. s->misc_bits += get_bits_diff(s);
  678. s->i_count++;
  679. } else if (s->mb_intra) {
  680. if (s->dquant && cbp) {
  681. put_mb_modes(s, 6, 0x01, 0, 0);
  682. put_qscale(s);
  683. } else {
  684. put_mb_modes(s, 5, 0x03, 0, 0);
  685. s->qscale -= s->dquant;
  686. }
  687. s->misc_bits += get_bits_diff(s);
  688. s->i_count++;
  689. memset(s->last_mv, 0, sizeof(s->last_mv));
  690. } else if (s->pict_type == AV_PICTURE_TYPE_P) {
  691. if (s->mv_type == MV_TYPE_16X16) {
  692. if (cbp != 0) {
  693. if ((motion_x | motion_y) == 0) {
  694. if (s->dquant) {
  695. /* macroblock_pattern & quant */
  696. put_mb_modes(s, 5, 1, 0, 0);
  697. put_qscale(s);
  698. } else {
  699. /* macroblock_pattern only */
  700. put_mb_modes(s, 2, 1, 0, 0);
  701. }
  702. s->misc_bits += get_bits_diff(s);
  703. } else {
  704. if (s->dquant) {
  705. put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */
  706. put_qscale(s);
  707. } else {
  708. put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */
  709. }
  710. s->misc_bits += get_bits_diff(s);
  711. // RAL: f_code parameter added
  712. mpeg1_encode_motion(s,
  713. motion_x - s->last_mv[0][0][0],
  714. s->f_code);
  715. // RAL: f_code parameter added
  716. mpeg1_encode_motion(s,
  717. motion_y - s->last_mv[0][0][1],
  718. s->f_code);
  719. s->mv_bits += get_bits_diff(s);
  720. }
  721. } else {
  722. put_bits(&s->pb, 3, 1); /* motion only */
  723. if (!s->frame_pred_frame_dct)
  724. put_bits(&s->pb, 2, 2); /* motion_type: frame */
  725. s->misc_bits += get_bits_diff(s);
  726. // RAL: f_code parameter added
  727. mpeg1_encode_motion(s,
  728. motion_x - s->last_mv[0][0][0],
  729. s->f_code);
  730. // RAL: f_code parameter added
  731. mpeg1_encode_motion(s,
  732. motion_y - s->last_mv[0][0][1],
  733. s->f_code);
  734. s->qscale -= s->dquant;
  735. s->mv_bits += get_bits_diff(s);
  736. }
  737. s->last_mv[0][1][0] = s->last_mv[0][0][0] = motion_x;
  738. s->last_mv[0][1][1] = s->last_mv[0][0][1] = motion_y;
  739. } else {
  740. av_assert2(!s->frame_pred_frame_dct && s->mv_type == MV_TYPE_FIELD);
  741. if (cbp) {
  742. if (s->dquant) {
  743. put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */
  744. put_qscale(s);
  745. } else {
  746. put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */
  747. }
  748. } else {
  749. put_bits(&s->pb, 3, 1); /* motion only */
  750. put_bits(&s->pb, 2, 1); /* motion_type: field */
  751. s->qscale -= s->dquant;
  752. }
  753. s->misc_bits += get_bits_diff(s);
  754. for (i = 0; i < 2; i++) {
  755. put_bits(&s->pb, 1, s->field_select[0][i]);
  756. mpeg1_encode_motion(s,
  757. s->mv[0][i][0] - s->last_mv[0][i][0],
  758. s->f_code);
  759. mpeg1_encode_motion(s,
  760. s->mv[0][i][1] - (s->last_mv[0][i][1] >> 1),
  761. s->f_code);
  762. s->last_mv[0][i][0] = s->mv[0][i][0];
  763. s->last_mv[0][i][1] = 2 * s->mv[0][i][1];
  764. }
  765. s->mv_bits += get_bits_diff(s);
  766. }
  767. if (cbp) {
  768. if (s->chroma_y_shift) {
  769. put_bits(&s->pb,
  770. ff_mpeg12_mbPatTable[cbp][1],
  771. ff_mpeg12_mbPatTable[cbp][0]);
  772. } else {
  773. put_bits(&s->pb,
  774. ff_mpeg12_mbPatTable[cbp >> 2][1],
  775. ff_mpeg12_mbPatTable[cbp >> 2][0]);
  776. put_sbits(&s->pb, 2, cbp);
  777. }
  778. }
  779. s->f_count++;
  780. } else {
  781. if (s->mv_type == MV_TYPE_16X16) {
  782. if (cbp) { // With coded bloc pattern
  783. if (s->dquant) {
  784. if (s->mv_dir == MV_DIR_FORWARD)
  785. put_mb_modes(s, 6, 3, 1, 0);
  786. else
  787. put_mb_modes(s, 8 - s->mv_dir, 2, 1, 0);
  788. put_qscale(s);
  789. } else {
  790. put_mb_modes(s, 5 - s->mv_dir, 3, 1, 0);
  791. }
  792. } else { // No coded bloc pattern
  793. put_bits(&s->pb, 5 - s->mv_dir, 2);
  794. if (!s->frame_pred_frame_dct)
  795. put_bits(&s->pb, 2, 2); /* motion_type: frame */
  796. s->qscale -= s->dquant;
  797. }
  798. s->misc_bits += get_bits_diff(s);
  799. if (s->mv_dir & MV_DIR_FORWARD) {
  800. mpeg1_encode_motion(s,
  801. s->mv[0][0][0] - s->last_mv[0][0][0],
  802. s->f_code);
  803. mpeg1_encode_motion(s,
  804. s->mv[0][0][1] - s->last_mv[0][0][1],
  805. s->f_code);
  806. s->last_mv[0][0][0] =
  807. s->last_mv[0][1][0] = s->mv[0][0][0];
  808. s->last_mv[0][0][1] =
  809. s->last_mv[0][1][1] = s->mv[0][0][1];
  810. s->f_count++;
  811. }
  812. if (s->mv_dir & MV_DIR_BACKWARD) {
  813. mpeg1_encode_motion(s,
  814. s->mv[1][0][0] - s->last_mv[1][0][0],
  815. s->b_code);
  816. mpeg1_encode_motion(s,
  817. s->mv[1][0][1] - s->last_mv[1][0][1],
  818. s->b_code);
  819. s->last_mv[1][0][0] =
  820. s->last_mv[1][1][0] = s->mv[1][0][0];
  821. s->last_mv[1][0][1] =
  822. s->last_mv[1][1][1] = s->mv[1][0][1];
  823. s->b_count++;
  824. }
  825. } else {
  826. av_assert2(s->mv_type == MV_TYPE_FIELD);
  827. av_assert2(!s->frame_pred_frame_dct);
  828. if (cbp) { // With coded bloc pattern
  829. if (s->dquant) {
  830. if (s->mv_dir == MV_DIR_FORWARD)
  831. put_mb_modes(s, 6, 3, 1, 1);
  832. else
  833. put_mb_modes(s, 8 - s->mv_dir, 2, 1, 1);
  834. put_qscale(s);
  835. } else {
  836. put_mb_modes(s, 5 - s->mv_dir, 3, 1, 1);
  837. }
  838. } else { // No coded bloc pattern
  839. put_bits(&s->pb, 5 - s->mv_dir, 2);
  840. put_bits(&s->pb, 2, 1); /* motion_type: field */
  841. s->qscale -= s->dquant;
  842. }
  843. s->misc_bits += get_bits_diff(s);
  844. if (s->mv_dir & MV_DIR_FORWARD) {
  845. for (i = 0; i < 2; i++) {
  846. put_bits(&s->pb, 1, s->field_select[0][i]);
  847. mpeg1_encode_motion(s,
  848. s->mv[0][i][0] - s->last_mv[0][i][0],
  849. s->f_code);
  850. mpeg1_encode_motion(s,
  851. s->mv[0][i][1] - (s->last_mv[0][i][1] >> 1),
  852. s->f_code);
  853. s->last_mv[0][i][0] = s->mv[0][i][0];
  854. s->last_mv[0][i][1] = s->mv[0][i][1] * 2;
  855. }
  856. s->f_count++;
  857. }
  858. if (s->mv_dir & MV_DIR_BACKWARD) {
  859. for (i = 0; i < 2; i++) {
  860. put_bits(&s->pb, 1, s->field_select[1][i]);
  861. mpeg1_encode_motion(s,
  862. s->mv[1][i][0] - s->last_mv[1][i][0],
  863. s->b_code);
  864. mpeg1_encode_motion(s,
  865. s->mv[1][i][1] - (s->last_mv[1][i][1] >> 1),
  866. s->b_code);
  867. s->last_mv[1][i][0] = s->mv[1][i][0];
  868. s->last_mv[1][i][1] = s->mv[1][i][1] * 2;
  869. }
  870. s->b_count++;
  871. }
  872. }
  873. s->mv_bits += get_bits_diff(s);
  874. if (cbp) {
  875. if (s->chroma_y_shift) {
  876. put_bits(&s->pb,
  877. ff_mpeg12_mbPatTable[cbp][1],
  878. ff_mpeg12_mbPatTable[cbp][0]);
  879. } else {
  880. put_bits(&s->pb,
  881. ff_mpeg12_mbPatTable[cbp >> 2][1],
  882. ff_mpeg12_mbPatTable[cbp >> 2][0]);
  883. put_sbits(&s->pb, 2, cbp);
  884. }
  885. }
  886. }
  887. for (i = 0; i < mb_block_count; i++)
  888. if (cbp & (1 << (mb_block_count - 1 - i)))
  889. mpeg1_encode_block(s, block[i], i);
  890. s->mb_skip_run = 0;
  891. if (s->mb_intra)
  892. s->i_tex_bits += get_bits_diff(s);
  893. else
  894. s->p_tex_bits += get_bits_diff(s);
  895. }
  896. }
  897. void ff_mpeg1_encode_mb(MpegEncContext *s, int16_t block[8][64],
  898. int motion_x, int motion_y)
  899. {
  900. if (s->chroma_format == CHROMA_420)
  901. mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 6);
  902. else
  903. mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8);
  904. }
  905. av_cold void ff_mpeg1_encode_init(MpegEncContext *s)
  906. {
  907. static int done = 0;
  908. ff_mpeg12_common_init(s);
  909. if (!done) {
  910. int f_code;
  911. int mv;
  912. int i;
  913. done = 1;
  914. ff_init_rl(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
  915. ff_init_rl(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]);
  916. for (i = 0; i < 64; i++) {
  917. mpeg1_max_level[0][i] = ff_rl_mpeg1.max_level[0][i];
  918. mpeg1_index_run[0][i] = ff_rl_mpeg1.index_run[0][i];
  919. }
  920. init_uni_ac_vlc(&ff_rl_mpeg1, uni_mpeg1_ac_vlc_len);
  921. if (s->intra_vlc_format)
  922. init_uni_ac_vlc(&ff_rl_mpeg2, uni_mpeg2_ac_vlc_len);
  923. /* build unified dc encoding tables */
  924. for (i = -255; i < 256; i++) {
  925. int adiff, index;
  926. int bits, code;
  927. int diff = i;
  928. adiff = FFABS(diff);
  929. if (diff < 0)
  930. diff--;
  931. index = av_log2(2 * adiff);
  932. bits = ff_mpeg12_vlc_dc_lum_bits[index] + index;
  933. code = (ff_mpeg12_vlc_dc_lum_code[index] << index) +
  934. av_mod_uintp2(diff, index);
  935. mpeg1_lum_dc_uni[i + 255] = bits + (code << 8);
  936. bits = ff_mpeg12_vlc_dc_chroma_bits[index] + index;
  937. code = (ff_mpeg12_vlc_dc_chroma_code[index] << index) +
  938. av_mod_uintp2(diff, index);
  939. mpeg1_chr_dc_uni[i + 255] = bits + (code << 8);
  940. }
  941. for (f_code = 1; f_code <= MAX_FCODE; f_code++)
  942. for (mv = -MAX_MV; mv <= MAX_MV; mv++) {
  943. int len;
  944. if (mv == 0) {
  945. len = ff_mpeg12_mbMotionVectorTable[0][1];
  946. } else {
  947. int val, bit_size, code;
  948. bit_size = f_code - 1;
  949. val = mv;
  950. if (val < 0)
  951. val = -val;
  952. val--;
  953. code = (val >> bit_size) + 1;
  954. if (code < 17)
  955. len = ff_mpeg12_mbMotionVectorTable[code][1] +
  956. 1 + bit_size;
  957. else
  958. len = ff_mpeg12_mbMotionVectorTable[16][1] +
  959. 2 + bit_size;
  960. }
  961. mv_penalty[f_code][mv + MAX_MV] = len;
  962. }
  963. for (f_code = MAX_FCODE; f_code > 0; f_code--)
  964. for (mv = -(8 << f_code); mv < (8 << f_code); mv++)
  965. fcode_tab[mv + MAX_MV] = f_code;
  966. }
  967. s->me.mv_penalty = mv_penalty;
  968. s->fcode_tab = fcode_tab;
  969. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
  970. s->min_qcoeff = -255;
  971. s->max_qcoeff = 255;
  972. } else {
  973. s->min_qcoeff = -2047;
  974. s->max_qcoeff = 2047;
  975. }
  976. if (s->intra_vlc_format) {
  977. s->intra_ac_vlc_length =
  978. s->intra_ac_vlc_last_length = uni_mpeg2_ac_vlc_len;
  979. } else {
  980. s->intra_ac_vlc_length =
  981. s->intra_ac_vlc_last_length = uni_mpeg1_ac_vlc_len;
  982. }
  983. s->inter_ac_vlc_length =
  984. s->inter_ac_vlc_last_length = uni_mpeg1_ac_vlc_len;
  985. }
  986. #define OFFSET(x) offsetof(MpegEncContext, x)
  987. #define VE AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
  988. #define COMMON_OPTS \
  989. { "gop_timecode", "MPEG GOP Timecode in hh:mm:ss[:;.]ff format", \
  990. OFFSET(tc_opt_str), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, VE },\
  991. { "intra_vlc", "Use MPEG-2 intra VLC table.", \
  992. OFFSET(intra_vlc_format), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE }, \
  993. { "drop_frame_timecode", "Timecode is in drop frame format.", \
  994. OFFSET(drop_frame_timecode), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE }, \
  995. { "scan_offset", "Reserve space for SVCD scan offset user data.", \
  996. OFFSET(scan_offset), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
  997. static const AVOption mpeg1_options[] = {
  998. COMMON_OPTS
  999. FF_MPV_COMMON_OPTS
  1000. { NULL },
  1001. };
  1002. static const AVOption mpeg2_options[] = {
  1003. COMMON_OPTS
  1004. { "non_linear_quant", "Use nonlinear quantizer.", OFFSET(q_scale_type), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
  1005. { "alternate_scan", "Enable alternate scantable.", OFFSET(alternate_scan), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
  1006. { "seq_disp_ext", "Write sequence_display_extension blocks.", OFFSET(seq_disp_ext), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE, "seq_disp_ext" },
  1007. { "auto", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = -1}, 0, 0, VE, "seq_disp_ext" },
  1008. { "never", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 0, VE, "seq_disp_ext" },
  1009. { "always", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 0, VE, "seq_disp_ext" },
  1010. FF_MPV_COMMON_OPTS
  1011. { NULL },
  1012. };
  1013. #define mpeg12_class(x) \
  1014. static const AVClass mpeg ## x ## _class = { \
  1015. .class_name = "mpeg" # x "video encoder", \
  1016. .item_name = av_default_item_name, \
  1017. .option = mpeg ## x ## _options, \
  1018. .version = LIBAVUTIL_VERSION_INT, \
  1019. };
  1020. mpeg12_class(1)
  1021. mpeg12_class(2)
  1022. AVCodec ff_mpeg1video_encoder = {
  1023. .name = "mpeg1video",
  1024. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
  1025. .type = AVMEDIA_TYPE_VIDEO,
  1026. .id = AV_CODEC_ID_MPEG1VIDEO,
  1027. .priv_data_size = sizeof(MpegEncContext),
  1028. .init = encode_init,
  1029. .encode2 = ff_mpv_encode_picture,
  1030. .close = ff_mpv_encode_end,
  1031. .supported_framerates = ff_mpeg12_frame_rate_tab + 1,
  1032. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
  1033. AV_PIX_FMT_NONE },
  1034. .capabilities = CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
  1035. .priv_class = &mpeg1_class,
  1036. };
  1037. AVCodec ff_mpeg2video_encoder = {
  1038. .name = "mpeg2video",
  1039. .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
  1040. .type = AVMEDIA_TYPE_VIDEO,
  1041. .id = AV_CODEC_ID_MPEG2VIDEO,
  1042. .priv_data_size = sizeof(MpegEncContext),
  1043. .init = encode_init,
  1044. .encode2 = ff_mpv_encode_picture,
  1045. .close = ff_mpv_encode_end,
  1046. .supported_framerates = ff_mpeg2_frame_rate_tab,
  1047. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
  1048. AV_PIX_FMT_YUV422P,
  1049. AV_PIX_FMT_NONE },
  1050. .capabilities = CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
  1051. .priv_class = &mpeg2_class,
  1052. };