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.

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