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.

955 lines
34KB

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