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.

985 lines
36KB

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