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.

960 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
  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. if (s->height > 2800) {
  277. put_header(s, SLICE_MIN_START_CODE + (s->mb_y & 127));
  278. put_bits(&s->pb, 3, s->mb_y >> 7); /* slice_vertical_position_extension */
  279. } else {
  280. put_header(s, SLICE_MIN_START_CODE + s->mb_y);
  281. }
  282. put_qscale(s);
  283. put_bits(&s->pb, 1, 0); /* slice extra information */
  284. }
  285. void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
  286. {
  287. mpeg1_encode_sequence_header(s);
  288. /* mpeg1 picture header */
  289. put_header(s, PICTURE_START_CODE);
  290. /* temporal reference */
  291. // RAL: s->picture_number instead of s->fake_picture_number
  292. put_bits(&s->pb, 10, (s->picture_number -
  293. s->gop_picture_number) & 0x3ff);
  294. put_bits(&s->pb, 3, s->pict_type);
  295. s->vbv_delay_ptr= s->pb.buf + put_bits_count(&s->pb)/8;
  296. put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */
  297. // RAL: Forward f_code also needed for B frames
  298. if (s->pict_type == FF_P_TYPE || s->pict_type == FF_B_TYPE) {
  299. put_bits(&s->pb, 1, 0); /* half pel coordinates */
  300. if(s->codec_id == CODEC_ID_MPEG1VIDEO)
  301. put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
  302. else
  303. put_bits(&s->pb, 3, 7); /* forward_f_code */
  304. }
  305. // RAL: Backward f_code necessary for B frames
  306. if (s->pict_type == FF_B_TYPE) {
  307. put_bits(&s->pb, 1, 0); /* half pel coordinates */
  308. if(s->codec_id == CODEC_ID_MPEG1VIDEO)
  309. put_bits(&s->pb, 3, s->b_code); /* backward_f_code */
  310. else
  311. put_bits(&s->pb, 3, 7); /* backward_f_code */
  312. }
  313. put_bits(&s->pb, 1, 0); /* extra bit picture */
  314. s->frame_pred_frame_dct = 1;
  315. if(s->codec_id == CODEC_ID_MPEG2VIDEO){
  316. put_header(s, EXT_START_CODE);
  317. put_bits(&s->pb, 4, 8); //pic ext
  318. if (s->pict_type == FF_P_TYPE || s->pict_type == FF_B_TYPE) {
  319. put_bits(&s->pb, 4, s->f_code);
  320. put_bits(&s->pb, 4, s->f_code);
  321. }else{
  322. put_bits(&s->pb, 8, 255);
  323. }
  324. if (s->pict_type == FF_B_TYPE) {
  325. put_bits(&s->pb, 4, s->b_code);
  326. put_bits(&s->pb, 4, s->b_code);
  327. }else{
  328. put_bits(&s->pb, 8, 255);
  329. }
  330. put_bits(&s->pb, 2, s->intra_dc_precision);
  331. assert(s->picture_structure == PICT_FRAME);
  332. put_bits(&s->pb, 2, s->picture_structure);
  333. if (s->progressive_sequence) {
  334. put_bits(&s->pb, 1, 0); /* no repeat */
  335. } else {
  336. put_bits(&s->pb, 1, s->current_picture_ptr->top_field_first);
  337. }
  338. /* XXX: optimize the generation of this flag with entropy
  339. measures */
  340. s->frame_pred_frame_dct = s->progressive_sequence;
  341. put_bits(&s->pb, 1, s->frame_pred_frame_dct);
  342. put_bits(&s->pb, 1, s->concealment_motion_vectors);
  343. put_bits(&s->pb, 1, s->q_scale_type);
  344. put_bits(&s->pb, 1, s->intra_vlc_format);
  345. put_bits(&s->pb, 1, s->alternate_scan);
  346. put_bits(&s->pb, 1, s->repeat_first_field);
  347. s->progressive_frame = s->progressive_sequence;
  348. put_bits(&s->pb, 1, s->chroma_format == CHROMA_420 ? s->progressive_frame : 0); /* chroma_420_type */
  349. put_bits(&s->pb, 1, s->progressive_frame);
  350. put_bits(&s->pb, 1, 0); //composite_display_flag
  351. }
  352. if(s->flags & CODEC_FLAG_SVCD_SCAN_OFFSET){
  353. int i;
  354. put_header(s, USER_START_CODE);
  355. for(i=0; i<sizeof(svcd_scan_offset_placeholder); i++){
  356. put_bits(&s->pb, 8, svcd_scan_offset_placeholder[i]);
  357. }
  358. }
  359. s->mb_y=0;
  360. ff_mpeg1_encode_slice_header(s);
  361. }
  362. static inline void put_mb_modes(MpegEncContext *s, int n, int bits,
  363. int has_mv, int field_motion)
  364. {
  365. put_bits(&s->pb, n, bits);
  366. if (!s->frame_pred_frame_dct) {
  367. if (has_mv)
  368. put_bits(&s->pb, 2, 2 - field_motion); /* motion_type: frame/field */
  369. put_bits(&s->pb, 1, s->interlaced_dct);
  370. }
  371. }
  372. static av_always_inline void mpeg1_encode_mb_internal(MpegEncContext *s,
  373. DCTELEM block[6][64],
  374. int motion_x, int motion_y,
  375. int mb_block_count)
  376. {
  377. int i, cbp;
  378. const int mb_x = s->mb_x;
  379. const int mb_y = s->mb_y;
  380. const int first_mb= mb_x == s->resync_mb_x && mb_y == s->resync_mb_y;
  381. /* compute cbp */
  382. cbp = 0;
  383. for(i=0;i<mb_block_count;i++) {
  384. if (s->block_last_index[i] >= 0)
  385. cbp |= 1 << (mb_block_count - 1 - i);
  386. }
  387. if (cbp == 0 && !first_mb && s->mv_type == MV_TYPE_16X16 &&
  388. (mb_x != s->mb_width - 1 || (mb_y != s->mb_height - 1 && s->codec_id == CODEC_ID_MPEG1VIDEO)) &&
  389. ((s->pict_type == FF_P_TYPE && (motion_x | motion_y) == 0) ||
  390. (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) |
  391. ((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))) {
  392. s->mb_skip_run++;
  393. s->qscale -= s->dquant;
  394. s->skip_count++;
  395. s->misc_bits++;
  396. s->last_bits++;
  397. if(s->pict_type == FF_P_TYPE){
  398. s->last_mv[0][1][0]= s->last_mv[0][0][0]=
  399. s->last_mv[0][1][1]= s->last_mv[0][0][1]= 0;
  400. }
  401. } else {
  402. if(first_mb){
  403. assert(s->mb_skip_run == 0);
  404. encode_mb_skip_run(s, s->mb_x);
  405. }else{
  406. encode_mb_skip_run(s, s->mb_skip_run);
  407. }
  408. if (s->pict_type == FF_I_TYPE) {
  409. if(s->dquant && cbp){
  410. put_mb_modes(s, 2, 1, 0, 0); /* macroblock_type : macroblock_quant = 1 */
  411. put_qscale(s);
  412. }else{
  413. put_mb_modes(s, 1, 1, 0, 0); /* macroblock_type : macroblock_quant = 0 */
  414. s->qscale -= s->dquant;
  415. }
  416. s->misc_bits+= get_bits_diff(s);
  417. s->i_count++;
  418. } else if (s->mb_intra) {
  419. if(s->dquant && cbp){
  420. put_mb_modes(s, 6, 0x01, 0, 0);
  421. put_qscale(s);
  422. }else{
  423. put_mb_modes(s, 5, 0x03, 0, 0);
  424. s->qscale -= s->dquant;
  425. }
  426. s->misc_bits+= get_bits_diff(s);
  427. s->i_count++;
  428. memset(s->last_mv, 0, sizeof(s->last_mv));
  429. } else if (s->pict_type == FF_P_TYPE) {
  430. if(s->mv_type == MV_TYPE_16X16){
  431. if (cbp != 0) {
  432. if ((motion_x|motion_y) == 0) {
  433. if(s->dquant){
  434. put_mb_modes(s, 5, 1, 0, 0); /* macroblock_pattern & quant */
  435. put_qscale(s);
  436. }else{
  437. put_mb_modes(s, 2, 1, 0, 0); /* macroblock_pattern only */
  438. }
  439. s->misc_bits+= get_bits_diff(s);
  440. } else {
  441. if(s->dquant){
  442. put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */
  443. put_qscale(s);
  444. }else{
  445. put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */
  446. }
  447. s->misc_bits+= get_bits_diff(s);
  448. mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added
  449. mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added
  450. s->mv_bits+= get_bits_diff(s);
  451. }
  452. } else {
  453. put_bits(&s->pb, 3, 1); /* motion only */
  454. if (!s->frame_pred_frame_dct)
  455. put_bits(&s->pb, 2, 2); /* motion_type: frame */
  456. s->misc_bits+= get_bits_diff(s);
  457. mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added
  458. mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added
  459. s->qscale -= s->dquant;
  460. s->mv_bits+= get_bits_diff(s);
  461. }
  462. s->last_mv[0][1][0]= s->last_mv[0][0][0]= motion_x;
  463. s->last_mv[0][1][1]= s->last_mv[0][0][1]= motion_y;
  464. }else{
  465. assert(!s->frame_pred_frame_dct && s->mv_type == MV_TYPE_FIELD);
  466. if (cbp) {
  467. if(s->dquant){
  468. put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */
  469. put_qscale(s);
  470. }else{
  471. put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */
  472. }
  473. } else {
  474. put_bits(&s->pb, 3, 1); /* motion only */
  475. put_bits(&s->pb, 2, 1); /* motion_type: field */
  476. s->qscale -= s->dquant;
  477. }
  478. s->misc_bits+= get_bits_diff(s);
  479. for(i=0; i<2; i++){
  480. put_bits(&s->pb, 1, s->field_select[0][i]);
  481. mpeg1_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code);
  482. mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code);
  483. s->last_mv[0][i][0]= s->mv[0][i][0];
  484. s->last_mv[0][i][1]= 2*s->mv[0][i][1];
  485. }
  486. s->mv_bits+= get_bits_diff(s);
  487. }
  488. if(cbp) {
  489. if (s->chroma_y_shift) {
  490. put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp][1], ff_mpeg12_mbPatTable[cbp][0]);
  491. } else {
  492. put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp>>2][1], ff_mpeg12_mbPatTable[cbp>>2][0]);
  493. put_sbits(&s->pb, 2, cbp);
  494. }
  495. }
  496. s->f_count++;
  497. } else{
  498. if(s->mv_type == MV_TYPE_16X16){
  499. if (cbp){ // With coded bloc pattern
  500. if (s->dquant) {
  501. if(s->mv_dir == MV_DIR_FORWARD)
  502. put_mb_modes(s, 6, 3, 1, 0);
  503. else
  504. put_mb_modes(s, 8-s->mv_dir, 2, 1, 0);
  505. put_qscale(s);
  506. } else {
  507. put_mb_modes(s, 5-s->mv_dir, 3, 1, 0);
  508. }
  509. }else{ // No coded bloc pattern
  510. put_bits(&s->pb, 5-s->mv_dir, 2);
  511. if (!s->frame_pred_frame_dct)
  512. put_bits(&s->pb, 2, 2); /* motion_type: frame */
  513. s->qscale -= s->dquant;
  514. }
  515. s->misc_bits += get_bits_diff(s);
  516. if (s->mv_dir&MV_DIR_FORWARD){
  517. mpeg1_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code);
  518. mpeg1_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code);
  519. s->last_mv[0][0][0]=s->last_mv[0][1][0]= s->mv[0][0][0];
  520. s->last_mv[0][0][1]=s->last_mv[0][1][1]= s->mv[0][0][1];
  521. s->f_count++;
  522. }
  523. if (s->mv_dir&MV_DIR_BACKWARD){
  524. mpeg1_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code);
  525. mpeg1_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code);
  526. s->last_mv[1][0][0]=s->last_mv[1][1][0]= s->mv[1][0][0];
  527. s->last_mv[1][0][1]=s->last_mv[1][1][1]= s->mv[1][0][1];
  528. s->b_count++;
  529. }
  530. }else{
  531. assert(s->mv_type == MV_TYPE_FIELD);
  532. assert(!s->frame_pred_frame_dct);
  533. if (cbp){ // With coded bloc pattern
  534. if (s->dquant) {
  535. if(s->mv_dir == MV_DIR_FORWARD)
  536. put_mb_modes(s, 6, 3, 1, 1);
  537. else
  538. put_mb_modes(s, 8-s->mv_dir, 2, 1, 1);
  539. put_qscale(s);
  540. } else {
  541. put_mb_modes(s, 5-s->mv_dir, 3, 1, 1);
  542. }
  543. }else{ // No coded bloc pattern
  544. put_bits(&s->pb, 5-s->mv_dir, 2);
  545. put_bits(&s->pb, 2, 1); /* motion_type: field */
  546. s->qscale -= s->dquant;
  547. }
  548. s->misc_bits += get_bits_diff(s);
  549. if (s->mv_dir&MV_DIR_FORWARD){
  550. for(i=0; i<2; i++){
  551. put_bits(&s->pb, 1, s->field_select[0][i]);
  552. mpeg1_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code);
  553. mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code);
  554. s->last_mv[0][i][0]= s->mv[0][i][0];
  555. s->last_mv[0][i][1]= 2*s->mv[0][i][1];
  556. }
  557. s->f_count++;
  558. }
  559. if (s->mv_dir&MV_DIR_BACKWARD){
  560. for(i=0; i<2; i++){
  561. put_bits(&s->pb, 1, s->field_select[1][i]);
  562. mpeg1_encode_motion(s, s->mv[1][i][0] - s->last_mv[1][i][0] , s->b_code);
  563. mpeg1_encode_motion(s, s->mv[1][i][1] - (s->last_mv[1][i][1]>>1), s->b_code);
  564. s->last_mv[1][i][0]= s->mv[1][i][0];
  565. s->last_mv[1][i][1]= 2*s->mv[1][i][1];
  566. }
  567. s->b_count++;
  568. }
  569. }
  570. s->mv_bits += get_bits_diff(s);
  571. if(cbp) {
  572. if (s->chroma_y_shift) {
  573. put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp][1], ff_mpeg12_mbPatTable[cbp][0]);
  574. } else {
  575. put_bits(&s->pb, ff_mpeg12_mbPatTable[cbp>>2][1], ff_mpeg12_mbPatTable[cbp>>2][0]);
  576. put_sbits(&s->pb, 2, cbp);
  577. }
  578. }
  579. }
  580. for(i=0;i<mb_block_count;i++) {
  581. if (cbp & (1 << (mb_block_count - 1 - i))) {
  582. mpeg1_encode_block(s, block[i], i);
  583. }
  584. }
  585. s->mb_skip_run = 0;
  586. if(s->mb_intra)
  587. s->i_tex_bits+= get_bits_diff(s);
  588. else
  589. s->p_tex_bits+= get_bits_diff(s);
  590. }
  591. }
  592. void mpeg1_encode_mb(MpegEncContext *s, DCTELEM block[6][64], int motion_x, int motion_y)
  593. {
  594. if (s->chroma_format == CHROMA_420) mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 6);
  595. else mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8);
  596. }
  597. // RAL: Parameter added: f_or_b_code
  598. static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
  599. {
  600. if (val == 0) {
  601. /* zero vector */
  602. put_bits(&s->pb,
  603. ff_mpeg12_mbMotionVectorTable[0][1],
  604. ff_mpeg12_mbMotionVectorTable[0][0]);
  605. } else {
  606. int code, sign, bits;
  607. int bit_size = f_or_b_code - 1;
  608. int range = 1 << bit_size;
  609. /* modulo encoding */
  610. int l= INT_BIT - 5 - bit_size;
  611. val= (val<<l)>>l;
  612. if (val >= 0) {
  613. val--;
  614. code = (val >> bit_size) + 1;
  615. bits = val & (range - 1);
  616. sign = 0;
  617. } else {
  618. val = -val;
  619. val--;
  620. code = (val >> bit_size) + 1;
  621. bits = val & (range - 1);
  622. sign = 1;
  623. }
  624. assert(code > 0 && code <= 16);
  625. put_bits(&s->pb,
  626. ff_mpeg12_mbMotionVectorTable[code][1],
  627. ff_mpeg12_mbMotionVectorTable[code][0]);
  628. put_bits(&s->pb, 1, sign);
  629. if (bit_size > 0) {
  630. put_bits(&s->pb, bit_size, bits);
  631. }
  632. }
  633. }
  634. void ff_mpeg1_encode_init(MpegEncContext *s)
  635. {
  636. static int done=0;
  637. ff_mpeg12_common_init(s);
  638. if(!done){
  639. int f_code;
  640. int mv;
  641. int i;
  642. done=1;
  643. init_rl(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
  644. init_rl(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]);
  645. for(i=0; i<64; i++)
  646. {
  647. mpeg1_max_level[0][i]= ff_rl_mpeg1.max_level[0][i];
  648. mpeg1_index_run[0][i]= ff_rl_mpeg1.index_run[0][i];
  649. }
  650. init_uni_ac_vlc(&ff_rl_mpeg1, uni_mpeg1_ac_vlc_len);
  651. if(s->intra_vlc_format)
  652. init_uni_ac_vlc(&ff_rl_mpeg2, uni_mpeg2_ac_vlc_len);
  653. /* build unified dc encoding tables */
  654. for(i=-255; i<256; i++)
  655. {
  656. int adiff, index;
  657. int bits, code;
  658. int diff=i;
  659. adiff = FFABS(diff);
  660. if(diff<0) diff--;
  661. index = av_log2(2*adiff);
  662. bits= ff_mpeg12_vlc_dc_lum_bits[index] + index;
  663. code= (ff_mpeg12_vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1));
  664. mpeg1_lum_dc_uni[i+255]= bits + (code<<8);
  665. bits= ff_mpeg12_vlc_dc_chroma_bits[index] + index;
  666. code= (ff_mpeg12_vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1));
  667. mpeg1_chr_dc_uni[i+255]= bits + (code<<8);
  668. }
  669. for(f_code=1; f_code<=MAX_FCODE; f_code++){
  670. for(mv=-MAX_MV; mv<=MAX_MV; mv++){
  671. int len;
  672. if(mv==0) len= ff_mpeg12_mbMotionVectorTable[0][1];
  673. else{
  674. int val, bit_size, range, code;
  675. bit_size = f_code - 1;
  676. range = 1 << bit_size;
  677. val=mv;
  678. if (val < 0)
  679. val = -val;
  680. val--;
  681. code = (val >> bit_size) + 1;
  682. if(code<17){
  683. len= ff_mpeg12_mbMotionVectorTable[code][1] + 1 + bit_size;
  684. }else{
  685. len= ff_mpeg12_mbMotionVectorTable[16][1] + 2 + bit_size;
  686. }
  687. }
  688. mv_penalty[f_code][mv+MAX_MV]= len;
  689. }
  690. }
  691. for(f_code=MAX_FCODE; f_code>0; f_code--){
  692. for(mv=-(8<<f_code); mv<(8<<f_code); mv++){
  693. fcode_tab[mv+MAX_MV]= f_code;
  694. }
  695. }
  696. }
  697. s->me.mv_penalty= mv_penalty;
  698. s->fcode_tab= fcode_tab;
  699. if(s->codec_id == CODEC_ID_MPEG1VIDEO){
  700. s->min_qcoeff=-255;
  701. s->max_qcoeff= 255;
  702. }else{
  703. s->min_qcoeff=-2047;
  704. s->max_qcoeff= 2047;
  705. }
  706. if (s->intra_vlc_format) {
  707. s->intra_ac_vlc_length=
  708. s->intra_ac_vlc_last_length= uni_mpeg2_ac_vlc_len;
  709. } else {
  710. s->intra_ac_vlc_length=
  711. s->intra_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
  712. }
  713. s->inter_ac_vlc_length=
  714. s->inter_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
  715. }
  716. static inline void encode_dc(MpegEncContext *s, int diff, int component)
  717. {
  718. if(((unsigned) (diff+255)) >= 511){
  719. int index;
  720. if(diff<0){
  721. index= av_log2_16bit(-2*diff);
  722. diff--;
  723. }else{
  724. index= av_log2_16bit(2*diff);
  725. }
  726. if (component == 0) {
  727. put_bits(
  728. &s->pb,
  729. ff_mpeg12_vlc_dc_lum_bits[index] + index,
  730. (ff_mpeg12_vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1)));
  731. }else{
  732. put_bits(
  733. &s->pb,
  734. ff_mpeg12_vlc_dc_chroma_bits[index] + index,
  735. (ff_mpeg12_vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1)));
  736. }
  737. }else{
  738. if (component == 0) {
  739. put_bits(
  740. &s->pb,
  741. mpeg1_lum_dc_uni[diff+255]&0xFF,
  742. mpeg1_lum_dc_uni[diff+255]>>8);
  743. } else {
  744. put_bits(
  745. &s->pb,
  746. mpeg1_chr_dc_uni[diff+255]&0xFF,
  747. mpeg1_chr_dc_uni[diff+255]>>8);
  748. }
  749. }
  750. }
  751. static void mpeg1_encode_block(MpegEncContext *s,
  752. DCTELEM *block,
  753. int n)
  754. {
  755. int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
  756. int code, component;
  757. const uint16_t (*table_vlc)[2] = ff_rl_mpeg1.table_vlc;
  758. last_index = s->block_last_index[n];
  759. /* DC coef */
  760. if (s->mb_intra) {
  761. component = (n <= 3 ? 0 : (n&1) + 1);
  762. dc = block[0]; /* overflow is impossible */
  763. diff = dc - s->last_dc[component];
  764. encode_dc(s, diff, component);
  765. s->last_dc[component] = dc;
  766. i = 1;
  767. if (s->intra_vlc_format)
  768. table_vlc = ff_rl_mpeg2.table_vlc;
  769. } else {
  770. /* encode the first coefficient : needs to be done here because
  771. it is handled slightly differently */
  772. level = block[0];
  773. if (abs(level) == 1) {
  774. code = ((uint32_t)level >> 31); /* the sign bit */
  775. put_bits(&s->pb, 2, code | 0x02);
  776. i = 1;
  777. } else {
  778. i = 0;
  779. last_non_zero = -1;
  780. goto next_coef;
  781. }
  782. }
  783. /* now quantify & encode AC coefs */
  784. last_non_zero = i - 1;
  785. for(;i<=last_index;i++) {
  786. j = s->intra_scantable.permutated[i];
  787. level = block[j];
  788. next_coef:
  789. #if 0
  790. if (level != 0)
  791. dprintf(s->avctx, "level[%d]=%d\n", i, level);
  792. #endif
  793. /* encode using VLC */
  794. if (level != 0) {
  795. run = i - last_non_zero - 1;
  796. alevel= level;
  797. MASK_ABS(sign, alevel)
  798. sign&=1;
  799. if (alevel <= mpeg1_max_level[0][run]){
  800. code= mpeg1_index_run[0][run] + alevel - 1;
  801. /* store the vlc & sign at once */
  802. put_bits(&s->pb, table_vlc[code][1]+1, (table_vlc[code][0]<<1) + sign);
  803. } else {
  804. /* escape seems to be pretty rare <5% so I do not optimize it */
  805. put_bits(&s->pb, table_vlc[111][1], table_vlc[111][0]);
  806. /* escape: only clip in this case */
  807. put_bits(&s->pb, 6, run);
  808. if(s->codec_id == CODEC_ID_MPEG1VIDEO){
  809. if (alevel < 128) {
  810. put_sbits(&s->pb, 8, level);
  811. } else {
  812. if (level < 0) {
  813. put_bits(&s->pb, 16, 0x8001 + level + 255);
  814. } else {
  815. put_sbits(&s->pb, 16, level);
  816. }
  817. }
  818. }else{
  819. put_sbits(&s->pb, 12, level);
  820. }
  821. }
  822. last_non_zero = i;
  823. }
  824. }
  825. /* end of block */
  826. put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
  827. }
  828. AVCodec mpeg1video_encoder = {
  829. "mpeg1video",
  830. AVMEDIA_TYPE_VIDEO,
  831. CODEC_ID_MPEG1VIDEO,
  832. sizeof(MpegEncContext),
  833. encode_init,
  834. MPV_encode_picture,
  835. MPV_encode_end,
  836. .supported_framerates= ff_frame_rate_tab+1,
  837. .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
  838. .capabilities= CODEC_CAP_DELAY,
  839. .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
  840. };
  841. AVCodec mpeg2video_encoder = {
  842. "mpeg2video",
  843. AVMEDIA_TYPE_VIDEO,
  844. CODEC_ID_MPEG2VIDEO,
  845. sizeof(MpegEncContext),
  846. encode_init,
  847. MPV_encode_picture,
  848. MPV_encode_end,
  849. .supported_framerates= ff_frame_rate_tab+1,
  850. .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_NONE},
  851. .capabilities= CODEC_CAP_DELAY,
  852. .long_name= NULL_IF_CONFIG_SMALL("MPEG-2 video"),
  853. };