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.

1183 lines
46KB

  1. /*
  2. * MPEG-1/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. * MPEG-1/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, "MPEG-1/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. "MPEG-1/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. /* MPEG-1 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. s->video_format != VIDEO_FORMAT_UNSPECIFIED);
  298. if (s->seq_disp_ext == 1 || (s->seq_disp_ext == -1 && use_seq_disp_ext)) {
  299. put_header(s, EXT_START_CODE);
  300. put_bits(&s->pb, 4, 2); // sequence display extension
  301. put_bits(&s->pb, 3, s->video_format); // video_format
  302. put_bits(&s->pb, 1, 1); // colour_description
  303. put_bits(&s->pb, 8, s->avctx->color_primaries); // colour_primaries
  304. put_bits(&s->pb, 8, s->avctx->color_trc); // transfer_characteristics
  305. put_bits(&s->pb, 8, s->avctx->colorspace); // matrix_coefficients
  306. put_bits(&s->pb, 14, width); // display_horizontal_size
  307. put_bits(&s->pb, 1, 1); // marker_bit
  308. put_bits(&s->pb, 14, height); // display_vertical_size
  309. put_bits(&s->pb, 3, 0); // remaining 3 bits are zero padding
  310. }
  311. }
  312. put_header(s, GOP_START_CODE);
  313. put_bits(&s->pb, 1, s->drop_frame_timecode); // drop frame flag
  314. /* time code: we must convert from the real frame rate to a
  315. * fake MPEG frame rate in case of low frame rate */
  316. fps = (framerate.num + framerate.den / 2) / framerate.den;
  317. time_code = s->current_picture_ptr->f->coded_picture_number +
  318. s->timecode_frame_start;
  319. s->gop_picture_number = s->current_picture_ptr->f->coded_picture_number;
  320. av_assert0(s->drop_frame_timecode == !!(s->tc.flags & AV_TIMECODE_FLAG_DROPFRAME));
  321. if (s->drop_frame_timecode)
  322. time_code = av_timecode_adjust_ntsc_framenum2(time_code, fps);
  323. put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24));
  324. put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60));
  325. put_bits(&s->pb, 1, 1);
  326. put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60));
  327. put_bits(&s->pb, 6, (uint32_t)((time_code % fps)));
  328. put_bits(&s->pb, 1, !!(s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP) || s->intra_only || !s->gop_picture_number);
  329. put_bits(&s->pb, 1, 0); // broken link
  330. }
  331. }
  332. static inline void encode_mb_skip_run(MpegEncContext *s, int run)
  333. {
  334. while (run >= 33) {
  335. put_bits(&s->pb, 11, 0x008);
  336. run -= 33;
  337. }
  338. put_bits(&s->pb, ff_mpeg12_mbAddrIncrTable[run][1],
  339. ff_mpeg12_mbAddrIncrTable[run][0]);
  340. }
  341. static av_always_inline void put_qscale(MpegEncContext *s)
  342. {
  343. put_bits(&s->pb, 5, s->qscale);
  344. }
  345. void ff_mpeg1_encode_slice_header(MpegEncContext *s)
  346. {
  347. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->height > 2800) {
  348. put_header(s, SLICE_MIN_START_CODE + (s->mb_y & 127));
  349. /* slice_vertical_position_extension */
  350. put_bits(&s->pb, 3, s->mb_y >> 7);
  351. } else {
  352. put_header(s, SLICE_MIN_START_CODE + s->mb_y);
  353. }
  354. put_qscale(s);
  355. /* slice extra information */
  356. put_bits(&s->pb, 1, 0);
  357. }
  358. void ff_mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
  359. {
  360. AVFrameSideData *side_data;
  361. mpeg1_encode_sequence_header(s);
  362. /* MPEG-1 picture header */
  363. put_header(s, PICTURE_START_CODE);
  364. /* temporal reference */
  365. // RAL: s->picture_number instead of s->fake_picture_number
  366. put_bits(&s->pb, 10,
  367. (s->picture_number - s->gop_picture_number) & 0x3ff);
  368. put_bits(&s->pb, 3, s->pict_type);
  369. s->vbv_delay_ptr = s->pb.buf + put_bits_count(&s->pb) / 8;
  370. put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */
  371. // RAL: Forward f_code also needed for B-frames
  372. if (s->pict_type == AV_PICTURE_TYPE_P ||
  373. s->pict_type == AV_PICTURE_TYPE_B) {
  374. put_bits(&s->pb, 1, 0); /* half pel coordinates */
  375. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO)
  376. put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
  377. else
  378. put_bits(&s->pb, 3, 7); /* forward_f_code */
  379. }
  380. // RAL: Backward f_code necessary for B-frames
  381. if (s->pict_type == AV_PICTURE_TYPE_B) {
  382. put_bits(&s->pb, 1, 0); /* half pel coordinates */
  383. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO)
  384. put_bits(&s->pb, 3, s->b_code); /* backward_f_code */
  385. else
  386. put_bits(&s->pb, 3, 7); /* backward_f_code */
  387. }
  388. put_bits(&s->pb, 1, 0); /* extra bit picture */
  389. s->frame_pred_frame_dct = 1;
  390. if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  391. put_header(s, EXT_START_CODE);
  392. put_bits(&s->pb, 4, 8); /* pic ext */
  393. if (s->pict_type == AV_PICTURE_TYPE_P ||
  394. s->pict_type == AV_PICTURE_TYPE_B) {
  395. put_bits(&s->pb, 4, s->f_code);
  396. put_bits(&s->pb, 4, s->f_code);
  397. } else {
  398. put_bits(&s->pb, 8, 255);
  399. }
  400. if (s->pict_type == AV_PICTURE_TYPE_B) {
  401. put_bits(&s->pb, 4, s->b_code);
  402. put_bits(&s->pb, 4, s->b_code);
  403. } else {
  404. put_bits(&s->pb, 8, 255);
  405. }
  406. put_bits(&s->pb, 2, s->intra_dc_precision);
  407. av_assert0(s->picture_structure == PICT_FRAME);
  408. put_bits(&s->pb, 2, s->picture_structure);
  409. if (s->progressive_sequence)
  410. put_bits(&s->pb, 1, 0); /* no repeat */
  411. else
  412. put_bits(&s->pb, 1, s->current_picture_ptr->f->top_field_first);
  413. /* XXX: optimize the generation of this flag with entropy measures */
  414. s->frame_pred_frame_dct = s->progressive_sequence;
  415. put_bits(&s->pb, 1, s->frame_pred_frame_dct);
  416. put_bits(&s->pb, 1, s->concealment_motion_vectors);
  417. put_bits(&s->pb, 1, s->q_scale_type);
  418. put_bits(&s->pb, 1, s->intra_vlc_format);
  419. put_bits(&s->pb, 1, s->alternate_scan);
  420. put_bits(&s->pb, 1, s->repeat_first_field);
  421. s->progressive_frame = s->progressive_sequence;
  422. /* chroma_420_type */
  423. put_bits(&s->pb, 1, s->chroma_format ==
  424. CHROMA_420 ? s->progressive_frame : 0);
  425. put_bits(&s->pb, 1, s->progressive_frame);
  426. put_bits(&s->pb, 1, 0); /* composite_display_flag */
  427. }
  428. if (s->scan_offset) {
  429. int i;
  430. put_header(s, USER_START_CODE);
  431. for (i = 0; i < sizeof(svcd_scan_offset_placeholder); i++)
  432. put_bits(&s->pb, 8, svcd_scan_offset_placeholder[i]);
  433. }
  434. side_data = av_frame_get_side_data(s->current_picture_ptr->f,
  435. AV_FRAME_DATA_STEREO3D);
  436. if (side_data) {
  437. AVStereo3D *stereo = (AVStereo3D *)side_data->data;
  438. uint8_t fpa_type;
  439. switch (stereo->type) {
  440. case AV_STEREO3D_SIDEBYSIDE:
  441. fpa_type = 0x03;
  442. break;
  443. case AV_STEREO3D_TOPBOTTOM:
  444. fpa_type = 0x04;
  445. break;
  446. case AV_STEREO3D_2D:
  447. fpa_type = 0x08;
  448. break;
  449. case AV_STEREO3D_SIDEBYSIDE_QUINCUNX:
  450. fpa_type = 0x23;
  451. break;
  452. default:
  453. fpa_type = 0;
  454. break;
  455. }
  456. if (fpa_type != 0) {
  457. put_header(s, USER_START_CODE);
  458. put_bits(&s->pb, 8, 'J'); // S3D_video_format_signaling_identifier
  459. put_bits(&s->pb, 8, 'P');
  460. put_bits(&s->pb, 8, '3');
  461. put_bits(&s->pb, 8, 'D');
  462. put_bits(&s->pb, 8, 0x03); // S3D_video_format_length
  463. put_bits(&s->pb, 1, 1); // reserved_bit
  464. put_bits(&s->pb, 7, fpa_type); // S3D_video_format_type
  465. put_bits(&s->pb, 8, 0x04); // reserved_data[0]
  466. put_bits(&s->pb, 8, 0xFF); // reserved_data[1]
  467. }
  468. }
  469. s->mb_y = 0;
  470. ff_mpeg1_encode_slice_header(s);
  471. }
  472. static inline void put_mb_modes(MpegEncContext *s, int n, int bits,
  473. int has_mv, int field_motion)
  474. {
  475. put_bits(&s->pb, n, bits);
  476. if (!s->frame_pred_frame_dct) {
  477. if (has_mv)
  478. /* motion_type: frame/field */
  479. put_bits(&s->pb, 2, 2 - field_motion);
  480. put_bits(&s->pb, 1, s->interlaced_dct);
  481. }
  482. }
  483. // RAL: Parameter added: f_or_b_code
  484. static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
  485. {
  486. if (val == 0) {
  487. /* zero vector */
  488. put_bits(&s->pb,
  489. ff_mpeg12_mbMotionVectorTable[0][1],
  490. ff_mpeg12_mbMotionVectorTable[0][0]);
  491. } else {
  492. int code, sign, bits;
  493. int bit_size = f_or_b_code - 1;
  494. int range = 1 << bit_size;
  495. /* modulo encoding */
  496. val = sign_extend(val, 5 + bit_size);
  497. if (val >= 0) {
  498. val--;
  499. code = (val >> bit_size) + 1;
  500. bits = val & (range - 1);
  501. sign = 0;
  502. } else {
  503. val = -val;
  504. val--;
  505. code = (val >> bit_size) + 1;
  506. bits = val & (range - 1);
  507. sign = 1;
  508. }
  509. av_assert2(code > 0 && code <= 16);
  510. put_bits(&s->pb,
  511. ff_mpeg12_mbMotionVectorTable[code][1],
  512. ff_mpeg12_mbMotionVectorTable[code][0]);
  513. put_bits(&s->pb, 1, sign);
  514. if (bit_size > 0)
  515. put_bits(&s->pb, bit_size, bits);
  516. }
  517. }
  518. static inline void encode_dc(MpegEncContext *s, int diff, int component)
  519. {
  520. unsigned int diff_u = diff + 255;
  521. if (diff_u >= 511) {
  522. int index;
  523. if (diff < 0) {
  524. index = av_log2_16bit(-2 * diff);
  525. diff--;
  526. } else {
  527. index = av_log2_16bit(2 * diff);
  528. }
  529. if (component == 0)
  530. put_bits(&s->pb,
  531. ff_mpeg12_vlc_dc_lum_bits[index] + index,
  532. (ff_mpeg12_vlc_dc_lum_code[index] << index) +
  533. av_mod_uintp2(diff, index));
  534. else
  535. put_bits(&s->pb,
  536. ff_mpeg12_vlc_dc_chroma_bits[index] + index,
  537. (ff_mpeg12_vlc_dc_chroma_code[index] << index) +
  538. av_mod_uintp2(diff, index));
  539. } else {
  540. if (component == 0)
  541. put_bits(&s->pb,
  542. mpeg1_lum_dc_uni[diff + 255] & 0xFF,
  543. mpeg1_lum_dc_uni[diff + 255] >> 8);
  544. else
  545. put_bits(&s->pb,
  546. mpeg1_chr_dc_uni[diff + 255] & 0xFF,
  547. mpeg1_chr_dc_uni[diff + 255] >> 8);
  548. }
  549. }
  550. static void mpeg1_encode_block(MpegEncContext *s, int16_t *block, int n)
  551. {
  552. int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
  553. int code, component;
  554. const uint16_t (*table_vlc)[2] = ff_rl_mpeg1.table_vlc;
  555. last_index = s->block_last_index[n];
  556. /* DC coef */
  557. if (s->mb_intra) {
  558. component = (n <= 3 ? 0 : (n & 1) + 1);
  559. dc = block[0]; /* overflow is impossible */
  560. diff = dc - s->last_dc[component];
  561. encode_dc(s, diff, component);
  562. s->last_dc[component] = dc;
  563. i = 1;
  564. if (s->intra_vlc_format)
  565. table_vlc = ff_rl_mpeg2.table_vlc;
  566. } else {
  567. /* encode the first coefficient: needs to be done here because
  568. * it is handled slightly differently */
  569. level = block[0];
  570. if (abs(level) == 1) {
  571. code = ((uint32_t)level >> 31); /* the sign bit */
  572. put_bits(&s->pb, 2, code | 0x02);
  573. i = 1;
  574. } else {
  575. i = 0;
  576. last_non_zero = -1;
  577. goto next_coef;
  578. }
  579. }
  580. /* now quantify & encode AC coefs */
  581. last_non_zero = i - 1;
  582. for (; i <= last_index; i++) {
  583. j = s->intra_scantable.permutated[i];
  584. level = block[j];
  585. next_coef:
  586. /* encode using VLC */
  587. if (level != 0) {
  588. run = i - last_non_zero - 1;
  589. alevel = level;
  590. MASK_ABS(sign, alevel);
  591. sign &= 1;
  592. if (alevel <= mpeg1_max_level[0][run]) {
  593. code = mpeg1_index_run[0][run] + alevel - 1;
  594. /* store the VLC & sign at once */
  595. put_bits(&s->pb, table_vlc[code][1] + 1,
  596. (table_vlc[code][0] << 1) + sign);
  597. } else {
  598. /* escape seems to be pretty rare <5% so I do not optimize it */
  599. put_bits(&s->pb, table_vlc[111][1], table_vlc[111][0]);
  600. /* escape: only clip in this case */
  601. put_bits(&s->pb, 6, run);
  602. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
  603. if (alevel < 128) {
  604. put_sbits(&s->pb, 8, level);
  605. } else {
  606. if (level < 0)
  607. put_bits(&s->pb, 16, 0x8001 + level + 255);
  608. else
  609. put_sbits(&s->pb, 16, level);
  610. }
  611. } else {
  612. put_sbits(&s->pb, 12, level);
  613. }
  614. }
  615. last_non_zero = i;
  616. }
  617. }
  618. /* end of block */
  619. put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
  620. }
  621. static av_always_inline void mpeg1_encode_mb_internal(MpegEncContext *s,
  622. int16_t block[8][64],
  623. int motion_x, int motion_y,
  624. int mb_block_count)
  625. {
  626. int i, cbp;
  627. const int mb_x = s->mb_x;
  628. const int mb_y = s->mb_y;
  629. const int first_mb = mb_x == s->resync_mb_x && mb_y == s->resync_mb_y;
  630. /* compute cbp */
  631. cbp = 0;
  632. for (i = 0; i < mb_block_count; i++)
  633. if (s->block_last_index[i] >= 0)
  634. cbp |= 1 << (mb_block_count - 1 - i);
  635. if (cbp == 0 && !first_mb && s->mv_type == MV_TYPE_16X16 &&
  636. (mb_x != s->mb_width - 1 ||
  637. (mb_y != s->end_mb_y - 1 && s->codec_id == AV_CODEC_ID_MPEG1VIDEO)) &&
  638. ((s->pict_type == AV_PICTURE_TYPE_P && (motion_x | motion_y) == 0) ||
  639. (s->pict_type == AV_PICTURE_TYPE_B && s->mv_dir == s->last_mv_dir &&
  640. (((s->mv_dir & MV_DIR_FORWARD)
  641. ? ((s->mv[0][0][0] - s->last_mv[0][0][0]) |
  642. (s->mv[0][0][1] - s->last_mv[0][0][1])) : 0) |
  643. ((s->mv_dir & MV_DIR_BACKWARD)
  644. ? ((s->mv[1][0][0] - s->last_mv[1][0][0]) |
  645. (s->mv[1][0][1] - s->last_mv[1][0][1])) : 0)) == 0))) {
  646. s->mb_skip_run++;
  647. s->qscale -= s->dquant;
  648. s->skip_count++;
  649. s->misc_bits++;
  650. s->last_bits++;
  651. if (s->pict_type == AV_PICTURE_TYPE_P) {
  652. s->last_mv[0][0][0] =
  653. s->last_mv[0][0][1] =
  654. s->last_mv[0][1][0] =
  655. s->last_mv[0][1][1] = 0;
  656. }
  657. } else {
  658. if (first_mb) {
  659. av_assert0(s->mb_skip_run == 0);
  660. encode_mb_skip_run(s, s->mb_x);
  661. } else {
  662. encode_mb_skip_run(s, s->mb_skip_run);
  663. }
  664. if (s->pict_type == AV_PICTURE_TYPE_I) {
  665. if (s->dquant && cbp) {
  666. /* macroblock_type: macroblock_quant = 1 */
  667. put_mb_modes(s, 2, 1, 0, 0);
  668. put_qscale(s);
  669. } else {
  670. /* macroblock_type: macroblock_quant = 0 */
  671. put_mb_modes(s, 1, 1, 0, 0);
  672. s->qscale -= s->dquant;
  673. }
  674. s->misc_bits += get_bits_diff(s);
  675. s->i_count++;
  676. } else if (s->mb_intra) {
  677. if (s->dquant && cbp) {
  678. put_mb_modes(s, 6, 0x01, 0, 0);
  679. put_qscale(s);
  680. } else {
  681. put_mb_modes(s, 5, 0x03, 0, 0);
  682. s->qscale -= s->dquant;
  683. }
  684. s->misc_bits += get_bits_diff(s);
  685. s->i_count++;
  686. memset(s->last_mv, 0, sizeof(s->last_mv));
  687. } else if (s->pict_type == AV_PICTURE_TYPE_P) {
  688. if (s->mv_type == MV_TYPE_16X16) {
  689. if (cbp != 0) {
  690. if ((motion_x | motion_y) == 0) {
  691. if (s->dquant) {
  692. /* macroblock_pattern & quant */
  693. put_mb_modes(s, 5, 1, 0, 0);
  694. put_qscale(s);
  695. } else {
  696. /* macroblock_pattern only */
  697. put_mb_modes(s, 2, 1, 0, 0);
  698. }
  699. s->misc_bits += get_bits_diff(s);
  700. } else {
  701. if (s->dquant) {
  702. put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */
  703. put_qscale(s);
  704. } else {
  705. put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */
  706. }
  707. s->misc_bits += get_bits_diff(s);
  708. // RAL: f_code parameter added
  709. mpeg1_encode_motion(s,
  710. motion_x - s->last_mv[0][0][0],
  711. s->f_code);
  712. // RAL: f_code parameter added
  713. mpeg1_encode_motion(s,
  714. motion_y - s->last_mv[0][0][1],
  715. s->f_code);
  716. s->mv_bits += get_bits_diff(s);
  717. }
  718. } else {
  719. put_bits(&s->pb, 3, 1); /* motion only */
  720. if (!s->frame_pred_frame_dct)
  721. put_bits(&s->pb, 2, 2); /* motion_type: frame */
  722. s->misc_bits += get_bits_diff(s);
  723. // RAL: f_code parameter added
  724. mpeg1_encode_motion(s,
  725. motion_x - s->last_mv[0][0][0],
  726. s->f_code);
  727. // RAL: f_code parameter added
  728. mpeg1_encode_motion(s,
  729. motion_y - s->last_mv[0][0][1],
  730. s->f_code);
  731. s->qscale -= s->dquant;
  732. s->mv_bits += get_bits_diff(s);
  733. }
  734. s->last_mv[0][1][0] = s->last_mv[0][0][0] = motion_x;
  735. s->last_mv[0][1][1] = s->last_mv[0][0][1] = motion_y;
  736. } else {
  737. av_assert2(!s->frame_pred_frame_dct && s->mv_type == MV_TYPE_FIELD);
  738. if (cbp) {
  739. if (s->dquant) {
  740. put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */
  741. put_qscale(s);
  742. } else {
  743. put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */
  744. }
  745. } else {
  746. put_bits(&s->pb, 3, 1); /* motion only */
  747. put_bits(&s->pb, 2, 1); /* motion_type: field */
  748. s->qscale -= s->dquant;
  749. }
  750. s->misc_bits += get_bits_diff(s);
  751. for (i = 0; i < 2; i++) {
  752. put_bits(&s->pb, 1, s->field_select[0][i]);
  753. mpeg1_encode_motion(s,
  754. s->mv[0][i][0] - s->last_mv[0][i][0],
  755. s->f_code);
  756. mpeg1_encode_motion(s,
  757. s->mv[0][i][1] - (s->last_mv[0][i][1] >> 1),
  758. s->f_code);
  759. s->last_mv[0][i][0] = s->mv[0][i][0];
  760. s->last_mv[0][i][1] = 2 * s->mv[0][i][1];
  761. }
  762. s->mv_bits += get_bits_diff(s);
  763. }
  764. if (cbp) {
  765. if (s->chroma_y_shift) {
  766. put_bits(&s->pb,
  767. ff_mpeg12_mbPatTable[cbp][1],
  768. ff_mpeg12_mbPatTable[cbp][0]);
  769. } else {
  770. put_bits(&s->pb,
  771. ff_mpeg12_mbPatTable[cbp >> 2][1],
  772. ff_mpeg12_mbPatTable[cbp >> 2][0]);
  773. put_sbits(&s->pb, 2, cbp);
  774. }
  775. }
  776. s->f_count++;
  777. } else {
  778. if (s->mv_type == MV_TYPE_16X16) {
  779. if (cbp) { // With coded bloc pattern
  780. if (s->dquant) {
  781. if (s->mv_dir == MV_DIR_FORWARD)
  782. put_mb_modes(s, 6, 3, 1, 0);
  783. else
  784. put_mb_modes(s, 8 - s->mv_dir, 2, 1, 0);
  785. put_qscale(s);
  786. } else {
  787. put_mb_modes(s, 5 - s->mv_dir, 3, 1, 0);
  788. }
  789. } else { // No coded bloc pattern
  790. put_bits(&s->pb, 5 - s->mv_dir, 2);
  791. if (!s->frame_pred_frame_dct)
  792. put_bits(&s->pb, 2, 2); /* motion_type: frame */
  793. s->qscale -= s->dquant;
  794. }
  795. s->misc_bits += get_bits_diff(s);
  796. if (s->mv_dir & MV_DIR_FORWARD) {
  797. mpeg1_encode_motion(s,
  798. s->mv[0][0][0] - s->last_mv[0][0][0],
  799. s->f_code);
  800. mpeg1_encode_motion(s,
  801. s->mv[0][0][1] - s->last_mv[0][0][1],
  802. s->f_code);
  803. s->last_mv[0][0][0] =
  804. s->last_mv[0][1][0] = s->mv[0][0][0];
  805. s->last_mv[0][0][1] =
  806. s->last_mv[0][1][1] = s->mv[0][0][1];
  807. s->f_count++;
  808. }
  809. if (s->mv_dir & MV_DIR_BACKWARD) {
  810. mpeg1_encode_motion(s,
  811. s->mv[1][0][0] - s->last_mv[1][0][0],
  812. s->b_code);
  813. mpeg1_encode_motion(s,
  814. s->mv[1][0][1] - s->last_mv[1][0][1],
  815. s->b_code);
  816. s->last_mv[1][0][0] =
  817. s->last_mv[1][1][0] = s->mv[1][0][0];
  818. s->last_mv[1][0][1] =
  819. s->last_mv[1][1][1] = s->mv[1][0][1];
  820. s->b_count++;
  821. }
  822. } else {
  823. av_assert2(s->mv_type == MV_TYPE_FIELD);
  824. av_assert2(!s->frame_pred_frame_dct);
  825. if (cbp) { // With coded bloc pattern
  826. if (s->dquant) {
  827. if (s->mv_dir == MV_DIR_FORWARD)
  828. put_mb_modes(s, 6, 3, 1, 1);
  829. else
  830. put_mb_modes(s, 8 - s->mv_dir, 2, 1, 1);
  831. put_qscale(s);
  832. } else {
  833. put_mb_modes(s, 5 - s->mv_dir, 3, 1, 1);
  834. }
  835. } else { // No coded bloc pattern
  836. put_bits(&s->pb, 5 - s->mv_dir, 2);
  837. put_bits(&s->pb, 2, 1); /* motion_type: field */
  838. s->qscale -= s->dquant;
  839. }
  840. s->misc_bits += get_bits_diff(s);
  841. if (s->mv_dir & MV_DIR_FORWARD) {
  842. for (i = 0; i < 2; i++) {
  843. put_bits(&s->pb, 1, s->field_select[0][i]);
  844. mpeg1_encode_motion(s,
  845. s->mv[0][i][0] - s->last_mv[0][i][0],
  846. s->f_code);
  847. mpeg1_encode_motion(s,
  848. s->mv[0][i][1] - (s->last_mv[0][i][1] >> 1),
  849. s->f_code);
  850. s->last_mv[0][i][0] = s->mv[0][i][0];
  851. s->last_mv[0][i][1] = s->mv[0][i][1] * 2;
  852. }
  853. s->f_count++;
  854. }
  855. if (s->mv_dir & MV_DIR_BACKWARD) {
  856. for (i = 0; i < 2; i++) {
  857. put_bits(&s->pb, 1, s->field_select[1][i]);
  858. mpeg1_encode_motion(s,
  859. s->mv[1][i][0] - s->last_mv[1][i][0],
  860. s->b_code);
  861. mpeg1_encode_motion(s,
  862. s->mv[1][i][1] - (s->last_mv[1][i][1] >> 1),
  863. s->b_code);
  864. s->last_mv[1][i][0] = s->mv[1][i][0];
  865. s->last_mv[1][i][1] = s->mv[1][i][1] * 2;
  866. }
  867. s->b_count++;
  868. }
  869. }
  870. s->mv_bits += get_bits_diff(s);
  871. if (cbp) {
  872. if (s->chroma_y_shift) {
  873. put_bits(&s->pb,
  874. ff_mpeg12_mbPatTable[cbp][1],
  875. ff_mpeg12_mbPatTable[cbp][0]);
  876. } else {
  877. put_bits(&s->pb,
  878. ff_mpeg12_mbPatTable[cbp >> 2][1],
  879. ff_mpeg12_mbPatTable[cbp >> 2][0]);
  880. put_sbits(&s->pb, 2, cbp);
  881. }
  882. }
  883. }
  884. for (i = 0; i < mb_block_count; i++)
  885. if (cbp & (1 << (mb_block_count - 1 - i)))
  886. mpeg1_encode_block(s, block[i], i);
  887. s->mb_skip_run = 0;
  888. if (s->mb_intra)
  889. s->i_tex_bits += get_bits_diff(s);
  890. else
  891. s->p_tex_bits += get_bits_diff(s);
  892. }
  893. }
  894. void ff_mpeg1_encode_mb(MpegEncContext *s, int16_t block[8][64],
  895. int motion_x, int motion_y)
  896. {
  897. if (s->chroma_format == CHROMA_420)
  898. mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 6);
  899. else
  900. mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8);
  901. }
  902. av_cold void ff_mpeg1_encode_init(MpegEncContext *s)
  903. {
  904. static int done = 0;
  905. ff_mpeg12_common_init(s);
  906. if (!done) {
  907. int f_code;
  908. int mv;
  909. int i;
  910. done = 1;
  911. ff_rl_init(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
  912. ff_rl_init(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]);
  913. for (i = 0; i < 64; i++) {
  914. mpeg1_max_level[0][i] = ff_rl_mpeg1.max_level[0][i];
  915. mpeg1_index_run[0][i] = ff_rl_mpeg1.index_run[0][i];
  916. }
  917. init_uni_ac_vlc(&ff_rl_mpeg1, uni_mpeg1_ac_vlc_len);
  918. if (s->intra_vlc_format)
  919. init_uni_ac_vlc(&ff_rl_mpeg2, uni_mpeg2_ac_vlc_len);
  920. /* build unified dc encoding tables */
  921. for (i = -255; i < 256; i++) {
  922. int adiff, index;
  923. int bits, code;
  924. int diff = i;
  925. adiff = FFABS(diff);
  926. if (diff < 0)
  927. diff--;
  928. index = av_log2(2 * adiff);
  929. bits = ff_mpeg12_vlc_dc_lum_bits[index] + index;
  930. code = (ff_mpeg12_vlc_dc_lum_code[index] << index) +
  931. av_mod_uintp2(diff, index);
  932. mpeg1_lum_dc_uni[i + 255] = bits + (code << 8);
  933. bits = ff_mpeg12_vlc_dc_chroma_bits[index] + index;
  934. code = (ff_mpeg12_vlc_dc_chroma_code[index] << index) +
  935. av_mod_uintp2(diff, index);
  936. mpeg1_chr_dc_uni[i + 255] = bits + (code << 8);
  937. }
  938. for (f_code = 1; f_code <= MAX_FCODE; f_code++)
  939. for (mv = -MAX_DMV; mv <= MAX_DMV; mv++) {
  940. int len;
  941. if (mv == 0) {
  942. len = ff_mpeg12_mbMotionVectorTable[0][1];
  943. } else {
  944. int val, bit_size, code;
  945. bit_size = f_code - 1;
  946. val = mv;
  947. if (val < 0)
  948. val = -val;
  949. val--;
  950. code = (val >> bit_size) + 1;
  951. if (code < 17)
  952. len = ff_mpeg12_mbMotionVectorTable[code][1] +
  953. 1 + bit_size;
  954. else
  955. len = ff_mpeg12_mbMotionVectorTable[16][1] +
  956. 2 + bit_size;
  957. }
  958. mv_penalty[f_code][mv + MAX_DMV] = len;
  959. }
  960. for (f_code = MAX_FCODE; f_code > 0; f_code--)
  961. for (mv = -(8 << f_code); mv < (8 << f_code); mv++)
  962. fcode_tab[mv + MAX_MV] = f_code;
  963. }
  964. s->me.mv_penalty = mv_penalty;
  965. s->fcode_tab = fcode_tab;
  966. if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
  967. s->min_qcoeff = -255;
  968. s->max_qcoeff = 255;
  969. } else {
  970. s->min_qcoeff = -2047;
  971. s->max_qcoeff = 2047;
  972. }
  973. if (s->intra_vlc_format) {
  974. s->intra_ac_vlc_length =
  975. s->intra_ac_vlc_last_length = uni_mpeg2_ac_vlc_len;
  976. } else {
  977. s->intra_ac_vlc_length =
  978. s->intra_ac_vlc_last_length = uni_mpeg1_ac_vlc_len;
  979. }
  980. s->inter_ac_vlc_length =
  981. s->inter_ac_vlc_last_length = uni_mpeg1_ac_vlc_len;
  982. }
  983. #define OFFSET(x) offsetof(MpegEncContext, x)
  984. #define VE AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM
  985. #define COMMON_OPTS \
  986. { "gop_timecode", "MPEG GOP Timecode in hh:mm:ss[:;.]ff format. Overrides timecode_frame_start.", \
  987. OFFSET(tc_opt_str), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, VE },\
  988. { "intra_vlc", "Use MPEG-2 intra VLC table.", \
  989. OFFSET(intra_vlc_format), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, \
  990. { "drop_frame_timecode", "Timecode is in drop frame format.", \
  991. OFFSET(drop_frame_timecode), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, \
  992. { "scan_offset", "Reserve space for SVCD scan offset user data.", \
  993. OFFSET(scan_offset), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, \
  994. { "timecode_frame_start", "GOP timecode frame start number, in non-drop-frame format", \
  995. OFFSET(timecode_frame_start), AV_OPT_TYPE_INT64, {.i64 = -1 }, -1, INT64_MAX, 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_BOOL, { .i64 = 0 }, 0, 1, VE },
  1004. { "alternate_scan", "Enable alternate scantable.", OFFSET(alternate_scan), AV_OPT_TYPE_BOOL, { .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. { "video_format", "Video_format in the sequence_display_extension indicating the source of the video.", OFFSET(video_format), AV_OPT_TYPE_INT, { .i64 = VIDEO_FORMAT_UNSPECIFIED }, 0, 7, VE, "video_format" },
  1010. { "component", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_COMPONENT }, 0, 0, VE, "video_format" },
  1011. { "pal", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_PAL }, 0, 0, VE, "video_format" },
  1012. { "ntsc", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_NTSC }, 0, 0, VE, "video_format" },
  1013. { "secam", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_SECAM }, 0, 0, VE, "video_format" },
  1014. { "mac", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_MAC }, 0, 0, VE, "video_format" },
  1015. { "unspecified", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VIDEO_FORMAT_UNSPECIFIED}, 0, 0, VE, "video_format" },
  1016. FF_MPV_COMMON_OPTS
  1017. { NULL },
  1018. };
  1019. #define mpeg12_class(x) \
  1020. static const AVClass mpeg ## x ## _class = { \
  1021. .class_name = "mpeg" # x "video encoder", \
  1022. .item_name = av_default_item_name, \
  1023. .option = mpeg ## x ## _options, \
  1024. .version = LIBAVUTIL_VERSION_INT, \
  1025. };
  1026. mpeg12_class(1)
  1027. mpeg12_class(2)
  1028. AVCodec ff_mpeg1video_encoder = {
  1029. .name = "mpeg1video",
  1030. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
  1031. .type = AVMEDIA_TYPE_VIDEO,
  1032. .id = AV_CODEC_ID_MPEG1VIDEO,
  1033. .priv_data_size = sizeof(MpegEncContext),
  1034. .init = encode_init,
  1035. .encode2 = ff_mpv_encode_picture,
  1036. .close = ff_mpv_encode_end,
  1037. .supported_framerates = ff_mpeg12_frame_rate_tab + 1,
  1038. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
  1039. AV_PIX_FMT_NONE },
  1040. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS,
  1041. .priv_class = &mpeg1_class,
  1042. };
  1043. AVCodec ff_mpeg2video_encoder = {
  1044. .name = "mpeg2video",
  1045. .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
  1046. .type = AVMEDIA_TYPE_VIDEO,
  1047. .id = AV_CODEC_ID_MPEG2VIDEO,
  1048. .priv_data_size = sizeof(MpegEncContext),
  1049. .init = encode_init,
  1050. .encode2 = ff_mpv_encode_picture,
  1051. .close = ff_mpv_encode_end,
  1052. .supported_framerates = ff_mpeg2_frame_rate_tab,
  1053. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
  1054. AV_PIX_FMT_YUV422P,
  1055. AV_PIX_FMT_NONE },
  1056. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS,
  1057. .priv_class = &mpeg2_class,
  1058. };