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.

995 lines
36KB

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