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.

977 lines
35KB

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