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.

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