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.

1217 lines
47KB

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