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.

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