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.

1230 lines
48KB

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