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.

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