mpeg12enc.c 34 KB

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