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.

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