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