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.

1055 lines
39KB

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