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.

3361 lines
114KB

  1. /*
  2. * MPEG1 codec / MPEG2 decoder
  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 mpeg12.c
  24. * MPEG1/2 codec
  25. */
  26. //#define DEBUG
  27. #include "avcodec.h"
  28. #include "dsputil.h"
  29. #include "mpegvideo.h"
  30. #include "mpeg12data.h"
  31. #include "bytestream.h"
  32. //#undef NDEBUG
  33. //#include <assert.h>
  34. #define DC_VLC_BITS 9
  35. #define MV_VLC_BITS 9
  36. #define MBINCR_VLC_BITS 9
  37. #define MB_PAT_VLC_BITS 9
  38. #define MB_PTYPE_VLC_BITS 6
  39. #define MB_BTYPE_VLC_BITS 6
  40. #define TEX_VLC_BITS 9
  41. #ifdef CONFIG_ENCODERS
  42. static void mpeg1_encode_block(MpegEncContext *s,
  43. DCTELEM *block,
  44. int component);
  45. static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code); // RAL: f_code parameter added
  46. #endif //CONFIG_ENCODERS
  47. static inline int mpeg1_decode_block_inter(MpegEncContext *s,
  48. DCTELEM *block,
  49. int n);
  50. static inline int mpeg1_decode_block_intra(MpegEncContext *s,
  51. DCTELEM *block,
  52. int n);
  53. static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n);
  54. static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
  55. DCTELEM *block,
  56. int n);
  57. static inline int mpeg2_decode_block_intra(MpegEncContext *s,
  58. DCTELEM *block,
  59. int n);
  60. static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s, DCTELEM *block, int n);
  61. static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n);
  62. static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred);
  63. static void exchange_uv(MpegEncContext *s);
  64. #ifdef HAVE_XVMC
  65. extern int XVMC_field_start(MpegEncContext *s, AVCodecContext *avctx);
  66. extern int XVMC_field_end(MpegEncContext *s);
  67. extern void XVMC_pack_pblocks(MpegEncContext *s,int cbp);
  68. extern void XVMC_init_block(MpegEncContext *s);//set s->block
  69. #endif
  70. static const enum PixelFormat pixfmt_yuv_420[]= {PIX_FMT_YUV420P,-1};
  71. static const enum PixelFormat pixfmt_yuv_422[]= {PIX_FMT_YUV422P,-1};
  72. static const enum PixelFormat pixfmt_yuv_444[]= {PIX_FMT_YUV444P,-1};
  73. static const enum PixelFormat pixfmt_xvmc_mpg2_420[] = {
  74. PIX_FMT_XVMC_MPEG2_IDCT,
  75. PIX_FMT_XVMC_MPEG2_MC,
  76. -1};
  77. #ifdef CONFIG_ENCODERS
  78. static uint8_t mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
  79. static uint8_t fcode_tab[MAX_MV*2+1];
  80. static uint8_t uni_mpeg1_ac_vlc_len [64*64*2];
  81. static uint8_t uni_mpeg2_ac_vlc_len [64*64*2];
  82. /* simple include everything table for dc, first byte is bits number next 3 are code*/
  83. static uint32_t mpeg1_lum_dc_uni[512];
  84. static uint32_t mpeg1_chr_dc_uni[512];
  85. static uint8_t mpeg1_index_run[2][64];
  86. static int8_t mpeg1_max_level[2][64];
  87. #endif //CONFIG_ENCODERS
  88. static uint8_t static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 3];
  89. static void init_2d_vlc_rl(RLTable *rl, int use_static)
  90. {
  91. int i;
  92. init_vlc(&rl->vlc, TEX_VLC_BITS, rl->n + 2,
  93. &rl->table_vlc[0][1], 4, 2,
  94. &rl->table_vlc[0][0], 4, 2, use_static);
  95. if(use_static)
  96. rl->rl_vlc[0]= av_mallocz_static(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
  97. else
  98. rl->rl_vlc[0]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
  99. for(i=0; i<rl->vlc.table_size; i++){
  100. int code= rl->vlc.table[i][0];
  101. int len = rl->vlc.table[i][1];
  102. int level, run;
  103. if(len==0){ // illegal code
  104. run= 65;
  105. level= MAX_LEVEL;
  106. }else if(len<0){ //more bits needed
  107. run= 0;
  108. level= code;
  109. }else{
  110. if(code==rl->n){ //esc
  111. run= 65;
  112. level= 0;
  113. }else if(code==rl->n+1){ //eob
  114. run= 0;
  115. level= 127;
  116. }else{
  117. run= rl->table_run [code] + 1;
  118. level= rl->table_level[code];
  119. }
  120. }
  121. rl->rl_vlc[0][i].len= len;
  122. rl->rl_vlc[0][i].level= level;
  123. rl->rl_vlc[0][i].run= run;
  124. }
  125. }
  126. #ifdef CONFIG_ENCODERS
  127. static void init_uni_ac_vlc(RLTable *rl, uint8_t *uni_ac_vlc_len){
  128. int i;
  129. for(i=0; i<128; i++){
  130. int level= i-64;
  131. int run;
  132. for(run=0; run<64; run++){
  133. int len, bits, code;
  134. int alevel= FFABS(level);
  135. int sign= (level>>31)&1;
  136. if (alevel > rl->max_level[0][run])
  137. code= 111; /*rl->n*/
  138. else
  139. code= rl->index_run[0][run] + alevel - 1;
  140. if (code < 111 /* rl->n */) {
  141. /* store the vlc & sign at once */
  142. len= rl->table_vlc[code][1]+1;
  143. bits= (rl->table_vlc[code][0]<<1) + sign;
  144. } else {
  145. len= rl->table_vlc[111/*rl->n*/][1]+6;
  146. bits= rl->table_vlc[111/*rl->n*/][0]<<6;
  147. bits|= run;
  148. if (alevel < 128) {
  149. bits<<=8; len+=8;
  150. bits|= level & 0xff;
  151. } else {
  152. bits<<=16; len+=16;
  153. bits|= level & 0xff;
  154. if (level < 0) {
  155. bits|= 0x8001 + level + 255;
  156. } else {
  157. bits|= level & 0xffff;
  158. }
  159. }
  160. }
  161. uni_ac_vlc_len [UNI_AC_ENC_INDEX(run, i)]= len;
  162. }
  163. }
  164. }
  165. static int find_frame_rate_index(MpegEncContext *s){
  166. int i;
  167. int64_t dmin= INT64_MAX;
  168. int64_t d;
  169. for(i=1;i<14;i++) {
  170. int64_t n0= 1001LL/ff_frame_rate_tab[i].den*ff_frame_rate_tab[i].num*s->avctx->time_base.num;
  171. int64_t n1= 1001LL*s->avctx->time_base.den;
  172. if(s->avctx->strict_std_compliance > FF_COMPLIANCE_INOFFICIAL && i>=9) break;
  173. d = FFABS(n0 - n1);
  174. if(d < dmin){
  175. dmin=d;
  176. s->frame_rate_index= i;
  177. }
  178. }
  179. if(dmin)
  180. return -1;
  181. else
  182. return 0;
  183. }
  184. static int encode_init(AVCodecContext *avctx)
  185. {
  186. MpegEncContext *s = avctx->priv_data;
  187. if(MPV_encode_init(avctx) < 0)
  188. return -1;
  189. if(find_frame_rate_index(s) < 0){
  190. if(s->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
  191. av_log(avctx, AV_LOG_ERROR, "MPEG1/2 does not support %d/%d fps\n", avctx->time_base.den, avctx->time_base.num);
  192. return -1;
  193. }else{
  194. 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);
  195. }
  196. }
  197. if(avctx->profile == FF_PROFILE_UNKNOWN){
  198. if(avctx->level != FF_LEVEL_UNKNOWN){
  199. av_log(avctx, AV_LOG_ERROR, "Set profile and level\n");
  200. return -1;
  201. }
  202. avctx->profile = s->chroma_format == CHROMA_420 ? 4 : 0; /* Main or 4:2:2 */
  203. }
  204. if(avctx->level == FF_LEVEL_UNKNOWN){
  205. if(avctx->profile == 0){ /* 4:2:2 */
  206. if(avctx->width <= 720 && avctx->height <= 608) avctx->level = 5; /* Main */
  207. else avctx->level = 2; /* High */
  208. }else{
  209. if(avctx->profile != 1 && s->chroma_format != CHROMA_420){
  210. av_log(avctx, AV_LOG_ERROR, "Only High(1) and 4:2:2(0) profiles support 4:2:2 color sampling\n");
  211. return -1;
  212. }
  213. if(avctx->width <= 720 && avctx->height <= 576) avctx->level = 8; /* Main */
  214. else if(avctx->width <= 1440) avctx->level = 6; /* High 1440 */
  215. else avctx->level = 4; /* High */
  216. }
  217. }
  218. if((avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE) && s->frame_rate_index != 4){
  219. av_log(avctx, AV_LOG_ERROR, "Drop frame time code only allowed with 1001/30000 fps\n");
  220. return -1;
  221. }
  222. return 0;
  223. }
  224. static void put_header(MpegEncContext *s, int header)
  225. {
  226. align_put_bits(&s->pb);
  227. put_bits(&s->pb, 16, header>>16);
  228. put_bits(&s->pb, 16, header&0xFFFF);
  229. }
  230. /* put sequence header if needed */
  231. static void mpeg1_encode_sequence_header(MpegEncContext *s)
  232. {
  233. unsigned int vbv_buffer_size;
  234. unsigned int fps, v;
  235. int i;
  236. uint64_t time_code;
  237. float best_aspect_error= 1E10;
  238. float aspect_ratio= av_q2d(s->avctx->sample_aspect_ratio);
  239. int constraint_parameter_flag;
  240. if(aspect_ratio==0.0) aspect_ratio= 1.0; //pixel aspect 1:1 (VGA)
  241. if (s->current_picture.key_frame) {
  242. AVRational framerate= ff_frame_rate_tab[s->frame_rate_index];
  243. /* mpeg1 header repeated every gop */
  244. put_header(s, SEQ_START_CODE);
  245. put_bits(&s->pb, 12, s->width);
  246. put_bits(&s->pb, 12, s->height);
  247. for(i=1; i<15; i++){
  248. float error= aspect_ratio;
  249. if(s->codec_id == CODEC_ID_MPEG1VIDEO || i <=1)
  250. error-= 1.0/mpeg1_aspect[i];
  251. else
  252. error-= av_q2d(mpeg2_aspect[i])*s->height/s->width;
  253. error= FFABS(error);
  254. if(error < best_aspect_error){
  255. best_aspect_error= error;
  256. s->aspect_ratio_info= i;
  257. }
  258. }
  259. put_bits(&s->pb, 4, s->aspect_ratio_info);
  260. put_bits(&s->pb, 4, s->frame_rate_index);
  261. if(s->avctx->rc_max_rate){
  262. v = (s->avctx->rc_max_rate + 399) / 400;
  263. if (v > 0x3ffff && s->codec_id == CODEC_ID_MPEG1VIDEO)
  264. v = 0x3ffff;
  265. }else{
  266. v= 0x3FFFF;
  267. }
  268. if(s->avctx->rc_buffer_size)
  269. vbv_buffer_size = s->avctx->rc_buffer_size;
  270. else
  271. /* VBV calculation: Scaled so that a VCD has the proper VBV size of 40 kilobytes */
  272. vbv_buffer_size = (( 20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024;
  273. vbv_buffer_size= (vbv_buffer_size + 16383) / 16384;
  274. put_bits(&s->pb, 18, v & 0x3FFFF);
  275. put_bits(&s->pb, 1, 1); /* marker */
  276. put_bits(&s->pb, 10, vbv_buffer_size & 0x3FF);
  277. constraint_parameter_flag=
  278. s->width <= 768 && s->height <= 576 &&
  279. s->mb_width * s->mb_height <= 396 &&
  280. s->mb_width * s->mb_height * framerate.num <= framerate.den*396*25 &&
  281. framerate.num <= framerate.den*30 &&
  282. s->avctx->me_range && s->avctx->me_range < 128 &&
  283. vbv_buffer_size <= 20 &&
  284. v <= 1856000/400 &&
  285. s->codec_id == CODEC_ID_MPEG1VIDEO;
  286. put_bits(&s->pb, 1, constraint_parameter_flag);
  287. ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
  288. ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
  289. if(s->codec_id == CODEC_ID_MPEG2VIDEO){
  290. put_header(s, EXT_START_CODE);
  291. put_bits(&s->pb, 4, 1); //seq ext
  292. put_bits(&s->pb, 1, s->avctx->profile == 0); //escx 1 for 4:2:2 profile */
  293. put_bits(&s->pb, 3, s->avctx->profile); //profile
  294. put_bits(&s->pb, 4, s->avctx->level); //level
  295. put_bits(&s->pb, 1, s->progressive_sequence);
  296. put_bits(&s->pb, 2, s->chroma_format);
  297. put_bits(&s->pb, 2, 0); //horizontal size ext
  298. put_bits(&s->pb, 2, 0); //vertical size ext
  299. put_bits(&s->pb, 12, v>>18); //bitrate ext
  300. put_bits(&s->pb, 1, 1); //marker
  301. put_bits(&s->pb, 8, vbv_buffer_size >>10); //vbv buffer ext
  302. put_bits(&s->pb, 1, s->low_delay);
  303. put_bits(&s->pb, 2, 0); // frame_rate_ext_n
  304. put_bits(&s->pb, 5, 0); // frame_rate_ext_d
  305. }
  306. put_header(s, GOP_START_CODE);
  307. put_bits(&s->pb, 1, !!(s->avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE)); /* drop frame flag */
  308. /* time code : we must convert from the real frame rate to a
  309. fake mpeg frame rate in case of low frame rate */
  310. fps = (framerate.num + framerate.den/2)/ framerate.den;
  311. time_code = s->current_picture_ptr->coded_picture_number + s->avctx->timecode_frame_start;
  312. s->gop_picture_number = s->current_picture_ptr->coded_picture_number;
  313. if (s->avctx->flags2 & CODEC_FLAG2_DROP_FRAME_TIMECODE) {
  314. /* only works for NTSC 29.97 */
  315. int d = time_code / 17982;
  316. int m = time_code % 17982;
  317. //if (m < 2) m += 2; /* not needed since -2,-1 / 1798 in C returns 0 */
  318. time_code += 18 * d + 2 * ((m - 2) / 1798);
  319. }
  320. put_bits(&s->pb, 5, (uint32_t)((time_code / (fps * 3600)) % 24));
  321. put_bits(&s->pb, 6, (uint32_t)((time_code / (fps * 60)) % 60));
  322. put_bits(&s->pb, 1, 1);
  323. put_bits(&s->pb, 6, (uint32_t)((time_code / fps) % 60));
  324. put_bits(&s->pb, 6, (uint32_t)((time_code % fps)));
  325. put_bits(&s->pb, 1, !!(s->flags & CODEC_FLAG_CLOSED_GOP));
  326. put_bits(&s->pb, 1, 0); /* broken link */
  327. }
  328. }
  329. static inline void encode_mb_skip_run(MpegEncContext *s, int run){
  330. while (run >= 33) {
  331. put_bits(&s->pb, 11, 0x008);
  332. run -= 33;
  333. }
  334. put_bits(&s->pb, mbAddrIncrTable[run][1],
  335. mbAddrIncrTable[run][0]);
  336. }
  337. #endif //CONFIG_ENCODERS
  338. static void common_init(MpegEncContext *s)
  339. {
  340. s->y_dc_scale_table=
  341. s->c_dc_scale_table= mpeg2_dc_scale_table[s->intra_dc_precision];
  342. }
  343. void ff_mpeg1_clean_buffers(MpegEncContext *s){
  344. s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
  345. s->last_dc[1] = s->last_dc[0];
  346. s->last_dc[2] = s->last_dc[0];
  347. memset(s->last_mv, 0, sizeof(s->last_mv));
  348. }
  349. #ifdef CONFIG_ENCODERS
  350. static av_always_inline void put_qscale(MpegEncContext *s)
  351. {
  352. if(s->q_scale_type){
  353. assert(s->qscale>=1 && s->qscale <=12);
  354. put_bits(&s->pb, 5, inv_non_linear_qscale[s->qscale]);
  355. }else{
  356. put_bits(&s->pb, 5, s->qscale);
  357. }
  358. }
  359. void ff_mpeg1_encode_slice_header(MpegEncContext *s){
  360. put_header(s, SLICE_MIN_START_CODE + s->mb_y);
  361. put_qscale(s);
  362. put_bits(&s->pb, 1, 0); /* slice extra information */
  363. }
  364. void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
  365. {
  366. mpeg1_encode_sequence_header(s);
  367. /* mpeg1 picture header */
  368. put_header(s, PICTURE_START_CODE);
  369. /* temporal reference */
  370. // RAL: s->picture_number instead of s->fake_picture_number
  371. put_bits(&s->pb, 10, (s->picture_number -
  372. s->gop_picture_number) & 0x3ff);
  373. put_bits(&s->pb, 3, s->pict_type);
  374. s->vbv_delay_ptr= s->pb.buf + put_bits_count(&s->pb)/8;
  375. put_bits(&s->pb, 16, 0xFFFF); /* vbv_delay */
  376. // RAL: Forward f_code also needed for B frames
  377. if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
  378. put_bits(&s->pb, 1, 0); /* half pel coordinates */
  379. if(s->codec_id == CODEC_ID_MPEG1VIDEO)
  380. put_bits(&s->pb, 3, s->f_code); /* forward_f_code */
  381. else
  382. put_bits(&s->pb, 3, 7); /* forward_f_code */
  383. }
  384. // RAL: Backward f_code necessary for B frames
  385. if (s->pict_type == B_TYPE) {
  386. put_bits(&s->pb, 1, 0); /* half pel coordinates */
  387. if(s->codec_id == CODEC_ID_MPEG1VIDEO)
  388. put_bits(&s->pb, 3, s->b_code); /* backward_f_code */
  389. else
  390. put_bits(&s->pb, 3, 7); /* backward_f_code */
  391. }
  392. put_bits(&s->pb, 1, 0); /* extra bit picture */
  393. s->frame_pred_frame_dct = 1;
  394. if(s->codec_id == CODEC_ID_MPEG2VIDEO){
  395. put_header(s, EXT_START_CODE);
  396. put_bits(&s->pb, 4, 8); //pic ext
  397. if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
  398. put_bits(&s->pb, 4, s->f_code);
  399. put_bits(&s->pb, 4, s->f_code);
  400. }else{
  401. put_bits(&s->pb, 8, 255);
  402. }
  403. if (s->pict_type == B_TYPE) {
  404. put_bits(&s->pb, 4, s->b_code);
  405. put_bits(&s->pb, 4, s->b_code);
  406. }else{
  407. put_bits(&s->pb, 8, 255);
  408. }
  409. put_bits(&s->pb, 2, s->intra_dc_precision);
  410. assert(s->picture_structure == PICT_FRAME);
  411. put_bits(&s->pb, 2, s->picture_structure);
  412. if (s->progressive_sequence) {
  413. put_bits(&s->pb, 1, 0); /* no repeat */
  414. } else {
  415. put_bits(&s->pb, 1, s->current_picture_ptr->top_field_first);
  416. }
  417. /* XXX: optimize the generation of this flag with entropy
  418. measures */
  419. s->frame_pred_frame_dct = s->progressive_sequence;
  420. put_bits(&s->pb, 1, s->frame_pred_frame_dct);
  421. put_bits(&s->pb, 1, s->concealment_motion_vectors);
  422. put_bits(&s->pb, 1, s->q_scale_type);
  423. put_bits(&s->pb, 1, s->intra_vlc_format);
  424. put_bits(&s->pb, 1, s->alternate_scan);
  425. put_bits(&s->pb, 1, s->repeat_first_field);
  426. s->progressive_frame = s->progressive_sequence;
  427. put_bits(&s->pb, 1, s->chroma_format == CHROMA_420 ? s->progressive_frame : 0); /* chroma_420_type */
  428. put_bits(&s->pb, 1, s->progressive_frame);
  429. put_bits(&s->pb, 1, 0); //composite_display_flag
  430. }
  431. if(s->flags & CODEC_FLAG_SVCD_SCAN_OFFSET){
  432. int i;
  433. put_header(s, USER_START_CODE);
  434. for(i=0; i<sizeof(svcd_scan_offset_placeholder); i++){
  435. put_bits(&s->pb, 8, svcd_scan_offset_placeholder[i]);
  436. }
  437. }
  438. s->mb_y=0;
  439. ff_mpeg1_encode_slice_header(s);
  440. }
  441. static inline void put_mb_modes(MpegEncContext *s, int n, int bits,
  442. int has_mv, int field_motion)
  443. {
  444. put_bits(&s->pb, n, bits);
  445. if (!s->frame_pred_frame_dct) {
  446. if (has_mv)
  447. put_bits(&s->pb, 2, 2 - field_motion); /* motion_type: frame/field */
  448. put_bits(&s->pb, 1, s->interlaced_dct);
  449. }
  450. }
  451. static av_always_inline void mpeg1_encode_mb_internal(MpegEncContext *s,
  452. DCTELEM block[6][64],
  453. int motion_x, int motion_y,
  454. int mb_block_count)
  455. {
  456. int i, cbp;
  457. const int mb_x = s->mb_x;
  458. const int mb_y = s->mb_y;
  459. const int first_mb= mb_x == s->resync_mb_x && mb_y == s->resync_mb_y;
  460. /* compute cbp */
  461. cbp = 0;
  462. for(i=0;i<mb_block_count;i++) {
  463. if (s->block_last_index[i] >= 0)
  464. cbp |= 1 << (mb_block_count - 1 - i);
  465. }
  466. if (cbp == 0 && !first_mb && s->mv_type == MV_TYPE_16X16 &&
  467. (mb_x != s->mb_width - 1 || (mb_y != s->mb_height - 1 && s->codec_id == CODEC_ID_MPEG1VIDEO)) &&
  468. ((s->pict_type == P_TYPE && (motion_x | motion_y) == 0) ||
  469. (s->pict_type == 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) |
  470. ((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))) {
  471. s->mb_skip_run++;
  472. s->qscale -= s->dquant;
  473. s->skip_count++;
  474. s->misc_bits++;
  475. s->last_bits++;
  476. if(s->pict_type == P_TYPE){
  477. s->last_mv[0][1][0]= s->last_mv[0][0][0]=
  478. s->last_mv[0][1][1]= s->last_mv[0][0][1]= 0;
  479. }
  480. } else {
  481. if(first_mb){
  482. assert(s->mb_skip_run == 0);
  483. encode_mb_skip_run(s, s->mb_x);
  484. }else{
  485. encode_mb_skip_run(s, s->mb_skip_run);
  486. }
  487. if (s->pict_type == I_TYPE) {
  488. if(s->dquant && cbp){
  489. put_mb_modes(s, 2, 1, 0, 0); /* macroblock_type : macroblock_quant = 1 */
  490. put_qscale(s);
  491. }else{
  492. put_mb_modes(s, 1, 1, 0, 0); /* macroblock_type : macroblock_quant = 0 */
  493. s->qscale -= s->dquant;
  494. }
  495. s->misc_bits+= get_bits_diff(s);
  496. s->i_count++;
  497. } else if (s->mb_intra) {
  498. if(s->dquant && cbp){
  499. put_mb_modes(s, 6, 0x01, 0, 0);
  500. put_qscale(s);
  501. }else{
  502. put_mb_modes(s, 5, 0x03, 0, 0);
  503. s->qscale -= s->dquant;
  504. }
  505. s->misc_bits+= get_bits_diff(s);
  506. s->i_count++;
  507. memset(s->last_mv, 0, sizeof(s->last_mv));
  508. } else if (s->pict_type == P_TYPE) {
  509. if(s->mv_type == MV_TYPE_16X16){
  510. if (cbp != 0) {
  511. if ((motion_x|motion_y) == 0) {
  512. if(s->dquant){
  513. put_mb_modes(s, 5, 1, 0, 0); /* macroblock_pattern & quant */
  514. put_qscale(s);
  515. }else{
  516. put_mb_modes(s, 2, 1, 0, 0); /* macroblock_pattern only */
  517. }
  518. s->misc_bits+= get_bits_diff(s);
  519. } else {
  520. if(s->dquant){
  521. put_mb_modes(s, 5, 2, 1, 0); /* motion + cbp */
  522. put_qscale(s);
  523. }else{
  524. put_mb_modes(s, 1, 1, 1, 0); /* motion + cbp */
  525. }
  526. s->misc_bits+= get_bits_diff(s);
  527. mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added
  528. mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added
  529. s->mv_bits+= get_bits_diff(s);
  530. }
  531. } else {
  532. put_bits(&s->pb, 3, 1); /* motion only */
  533. if (!s->frame_pred_frame_dct)
  534. put_bits(&s->pb, 2, 2); /* motion_type: frame */
  535. s->misc_bits+= get_bits_diff(s);
  536. mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0], s->f_code); // RAL: f_code parameter added
  537. mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1], s->f_code); // RAL: f_code parameter added
  538. s->qscale -= s->dquant;
  539. s->mv_bits+= get_bits_diff(s);
  540. }
  541. s->last_mv[0][1][0]= s->last_mv[0][0][0]= motion_x;
  542. s->last_mv[0][1][1]= s->last_mv[0][0][1]= motion_y;
  543. }else{
  544. assert(!s->frame_pred_frame_dct && s->mv_type == MV_TYPE_FIELD);
  545. if (cbp) {
  546. if(s->dquant){
  547. put_mb_modes(s, 5, 2, 1, 1); /* motion + cbp */
  548. put_qscale(s);
  549. }else{
  550. put_mb_modes(s, 1, 1, 1, 1); /* motion + cbp */
  551. }
  552. } else {
  553. put_bits(&s->pb, 3, 1); /* motion only */
  554. put_bits(&s->pb, 2, 1); /* motion_type: field */
  555. s->qscale -= s->dquant;
  556. }
  557. s->misc_bits+= get_bits_diff(s);
  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->mv_bits+= get_bits_diff(s);
  566. }
  567. if(cbp) {
  568. if (s->chroma_y_shift) {
  569. put_bits(&s->pb, mbPatTable[cbp][1], mbPatTable[cbp][0]);
  570. } else {
  571. put_bits(&s->pb, mbPatTable[cbp>>2][1], mbPatTable[cbp>>2][0]);
  572. put_bits(&s->pb, 2, cbp & 3);
  573. }
  574. }
  575. s->f_count++;
  576. } else{
  577. static const int mb_type_len[4]={0,3,4,2}; //bak,for,bi
  578. if(s->mv_type == MV_TYPE_16X16){
  579. if (cbp){ // With coded bloc pattern
  580. if (s->dquant) {
  581. if(s->mv_dir == MV_DIR_FORWARD)
  582. put_mb_modes(s, 6, 3, 1, 0);
  583. else
  584. put_mb_modes(s, mb_type_len[s->mv_dir]+3, 2, 1, 0);
  585. put_qscale(s);
  586. } else {
  587. put_mb_modes(s, mb_type_len[s->mv_dir], 3, 1, 0);
  588. }
  589. }else{ // No coded bloc pattern
  590. put_bits(&s->pb, mb_type_len[s->mv_dir], 2);
  591. if (!s->frame_pred_frame_dct)
  592. put_bits(&s->pb, 2, 2); /* motion_type: frame */
  593. s->qscale -= s->dquant;
  594. }
  595. s->misc_bits += get_bits_diff(s);
  596. if (s->mv_dir&MV_DIR_FORWARD){
  597. mpeg1_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code);
  598. mpeg1_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code);
  599. s->last_mv[0][0][0]=s->last_mv[0][1][0]= s->mv[0][0][0];
  600. s->last_mv[0][0][1]=s->last_mv[0][1][1]= s->mv[0][0][1];
  601. s->f_count++;
  602. }
  603. if (s->mv_dir&MV_DIR_BACKWARD){
  604. mpeg1_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code);
  605. mpeg1_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code);
  606. s->last_mv[1][0][0]=s->last_mv[1][1][0]= s->mv[1][0][0];
  607. s->last_mv[1][0][1]=s->last_mv[1][1][1]= s->mv[1][0][1];
  608. s->b_count++;
  609. }
  610. }else{
  611. assert(s->mv_type == MV_TYPE_FIELD);
  612. assert(!s->frame_pred_frame_dct);
  613. if (cbp){ // With coded bloc pattern
  614. if (s->dquant) {
  615. if(s->mv_dir == MV_DIR_FORWARD)
  616. put_mb_modes(s, 6, 3, 1, 1);
  617. else
  618. put_mb_modes(s, mb_type_len[s->mv_dir]+3, 2, 1, 1);
  619. put_qscale(s);
  620. } else {
  621. put_mb_modes(s, mb_type_len[s->mv_dir], 3, 1, 1);
  622. }
  623. }else{ // No coded bloc pattern
  624. put_bits(&s->pb, mb_type_len[s->mv_dir], 2);
  625. put_bits(&s->pb, 2, 1); /* motion_type: field */
  626. s->qscale -= s->dquant;
  627. }
  628. s->misc_bits += get_bits_diff(s);
  629. if (s->mv_dir&MV_DIR_FORWARD){
  630. for(i=0; i<2; i++){
  631. put_bits(&s->pb, 1, s->field_select[0][i]);
  632. mpeg1_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code);
  633. mpeg1_encode_motion(s, s->mv[0][i][1] - (s->last_mv[0][i][1]>>1), s->f_code);
  634. s->last_mv[0][i][0]= s->mv[0][i][0];
  635. s->last_mv[0][i][1]= 2*s->mv[0][i][1];
  636. }
  637. s->f_count++;
  638. }
  639. if (s->mv_dir&MV_DIR_BACKWARD){
  640. for(i=0; i<2; i++){
  641. put_bits(&s->pb, 1, s->field_select[1][i]);
  642. mpeg1_encode_motion(s, s->mv[1][i][0] - s->last_mv[1][i][0] , s->b_code);
  643. mpeg1_encode_motion(s, s->mv[1][i][1] - (s->last_mv[1][i][1]>>1), s->b_code);
  644. s->last_mv[1][i][0]= s->mv[1][i][0];
  645. s->last_mv[1][i][1]= 2*s->mv[1][i][1];
  646. }
  647. s->b_count++;
  648. }
  649. }
  650. s->mv_bits += get_bits_diff(s);
  651. if(cbp) {
  652. if (s->chroma_y_shift) {
  653. put_bits(&s->pb, mbPatTable[cbp][1], mbPatTable[cbp][0]);
  654. } else {
  655. put_bits(&s->pb, mbPatTable[cbp>>2][1], mbPatTable[cbp>>2][0]);
  656. put_bits(&s->pb, 2, cbp & 3);
  657. }
  658. }
  659. }
  660. for(i=0;i<mb_block_count;i++) {
  661. if (cbp & (1 << (mb_block_count - 1 - i))) {
  662. mpeg1_encode_block(s, block[i], i);
  663. }
  664. }
  665. s->mb_skip_run = 0;
  666. if(s->mb_intra)
  667. s->i_tex_bits+= get_bits_diff(s);
  668. else
  669. s->p_tex_bits+= get_bits_diff(s);
  670. }
  671. }
  672. void mpeg1_encode_mb(MpegEncContext *s, DCTELEM block[6][64], int motion_x, int motion_y)
  673. {
  674. if (s->chroma_format == CHROMA_420) mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 6);
  675. else mpeg1_encode_mb_internal(s, block, motion_x, motion_y, 8);
  676. }
  677. // RAL: Parameter added: f_or_b_code
  678. static void mpeg1_encode_motion(MpegEncContext *s, int val, int f_or_b_code)
  679. {
  680. int code, bit_size, l, bits, range, sign;
  681. if (val == 0) {
  682. /* zero vector */
  683. code = 0;
  684. put_bits(&s->pb,
  685. mbMotionVectorTable[0][1],
  686. mbMotionVectorTable[0][0]);
  687. } else {
  688. bit_size = f_or_b_code - 1;
  689. range = 1 << bit_size;
  690. /* modulo encoding */
  691. l= INT_BIT - 5 - bit_size;
  692. val= (val<<l)>>l;
  693. if (val >= 0) {
  694. val--;
  695. code = (val >> bit_size) + 1;
  696. bits = val & (range - 1);
  697. sign = 0;
  698. } else {
  699. val = -val;
  700. val--;
  701. code = (val >> bit_size) + 1;
  702. bits = val & (range - 1);
  703. sign = 1;
  704. }
  705. assert(code > 0 && code <= 16);
  706. put_bits(&s->pb,
  707. mbMotionVectorTable[code][1],
  708. mbMotionVectorTable[code][0]);
  709. put_bits(&s->pb, 1, sign);
  710. if (bit_size > 0) {
  711. put_bits(&s->pb, bit_size, bits);
  712. }
  713. }
  714. }
  715. void ff_mpeg1_encode_init(MpegEncContext *s)
  716. {
  717. static int done=0;
  718. common_init(s);
  719. if(!done){
  720. int f_code;
  721. int mv;
  722. int i;
  723. done=1;
  724. init_rl(&rl_mpeg1, static_rl_table_store[0]);
  725. init_rl(&rl_mpeg2, static_rl_table_store[1]);
  726. for(i=0; i<64; i++)
  727. {
  728. mpeg1_max_level[0][i]= rl_mpeg1.max_level[0][i];
  729. mpeg1_index_run[0][i]= rl_mpeg1.index_run[0][i];
  730. }
  731. init_uni_ac_vlc(&rl_mpeg1, uni_mpeg1_ac_vlc_len);
  732. if(s->intra_vlc_format)
  733. init_uni_ac_vlc(&rl_mpeg2, uni_mpeg2_ac_vlc_len);
  734. /* build unified dc encoding tables */
  735. for(i=-255; i<256; i++)
  736. {
  737. int adiff, index;
  738. int bits, code;
  739. int diff=i;
  740. adiff = FFABS(diff);
  741. if(diff<0) diff--;
  742. index = av_log2(2*adiff);
  743. bits= vlc_dc_lum_bits[index] + index;
  744. code= (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1));
  745. mpeg1_lum_dc_uni[i+255]= bits + (code<<8);
  746. bits= vlc_dc_chroma_bits[index] + index;
  747. code= (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1));
  748. mpeg1_chr_dc_uni[i+255]= bits + (code<<8);
  749. }
  750. for(f_code=1; f_code<=MAX_FCODE; f_code++){
  751. for(mv=-MAX_MV; mv<=MAX_MV; mv++){
  752. int len;
  753. if(mv==0) len= mbMotionVectorTable[0][1];
  754. else{
  755. int val, bit_size, range, code;
  756. bit_size = f_code - 1;
  757. range = 1 << bit_size;
  758. val=mv;
  759. if (val < 0)
  760. val = -val;
  761. val--;
  762. code = (val >> bit_size) + 1;
  763. if(code<17){
  764. len= mbMotionVectorTable[code][1] + 1 + bit_size;
  765. }else{
  766. len= mbMotionVectorTable[16][1] + 2 + bit_size;
  767. }
  768. }
  769. mv_penalty[f_code][mv+MAX_MV]= len;
  770. }
  771. }
  772. for(f_code=MAX_FCODE; f_code>0; f_code--){
  773. for(mv=-(8<<f_code); mv<(8<<f_code); mv++){
  774. fcode_tab[mv+MAX_MV]= f_code;
  775. }
  776. }
  777. }
  778. s->me.mv_penalty= mv_penalty;
  779. s->fcode_tab= fcode_tab;
  780. if(s->codec_id == CODEC_ID_MPEG1VIDEO){
  781. s->min_qcoeff=-255;
  782. s->max_qcoeff= 255;
  783. }else{
  784. s->min_qcoeff=-2047;
  785. s->max_qcoeff= 2047;
  786. }
  787. if (s->intra_vlc_format) {
  788. s->intra_ac_vlc_length=
  789. s->intra_ac_vlc_last_length= uni_mpeg2_ac_vlc_len;
  790. } else {
  791. s->intra_ac_vlc_length=
  792. s->intra_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
  793. }
  794. s->inter_ac_vlc_length=
  795. s->inter_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
  796. }
  797. static inline void encode_dc(MpegEncContext *s, int diff, int component)
  798. {
  799. if(((unsigned) (diff+255)) >= 511){
  800. int index;
  801. if(diff<0){
  802. index= av_log2_16bit(-2*diff);
  803. diff--;
  804. }else{
  805. index= av_log2_16bit(2*diff);
  806. }
  807. if (component == 0) {
  808. put_bits(
  809. &s->pb,
  810. vlc_dc_lum_bits[index] + index,
  811. (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1)));
  812. }else{
  813. put_bits(
  814. &s->pb,
  815. vlc_dc_chroma_bits[index] + index,
  816. (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1)));
  817. }
  818. }else{
  819. if (component == 0) {
  820. put_bits(
  821. &s->pb,
  822. mpeg1_lum_dc_uni[diff+255]&0xFF,
  823. mpeg1_lum_dc_uni[diff+255]>>8);
  824. } else {
  825. put_bits(
  826. &s->pb,
  827. mpeg1_chr_dc_uni[diff+255]&0xFF,
  828. mpeg1_chr_dc_uni[diff+255]>>8);
  829. }
  830. }
  831. }
  832. static void mpeg1_encode_block(MpegEncContext *s,
  833. DCTELEM *block,
  834. int n)
  835. {
  836. int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
  837. int code, component;
  838. const uint16_t (*table_vlc)[2] = rl_mpeg1.table_vlc;
  839. last_index = s->block_last_index[n];
  840. /* DC coef */
  841. if (s->mb_intra) {
  842. component = (n <= 3 ? 0 : (n&1) + 1);
  843. dc = block[0]; /* overflow is impossible */
  844. diff = dc - s->last_dc[component];
  845. encode_dc(s, diff, component);
  846. s->last_dc[component] = dc;
  847. i = 1;
  848. if (s->intra_vlc_format)
  849. table_vlc = rl_mpeg2.table_vlc;
  850. } else {
  851. /* encode the first coefficient : needs to be done here because
  852. it is handled slightly differently */
  853. level = block[0];
  854. if (abs(level) == 1) {
  855. code = ((uint32_t)level >> 31); /* the sign bit */
  856. put_bits(&s->pb, 2, code | 0x02);
  857. i = 1;
  858. } else {
  859. i = 0;
  860. last_non_zero = -1;
  861. goto next_coef;
  862. }
  863. }
  864. /* now quantify & encode AC coefs */
  865. last_non_zero = i - 1;
  866. for(;i<=last_index;i++) {
  867. j = s->intra_scantable.permutated[i];
  868. level = block[j];
  869. next_coef:
  870. #if 0
  871. if (level != 0)
  872. dprintf(s->avctx, "level[%d]=%d\n", i, level);
  873. #endif
  874. /* encode using VLC */
  875. if (level != 0) {
  876. run = i - last_non_zero - 1;
  877. alevel= level;
  878. MASK_ABS(sign, alevel)
  879. sign&=1;
  880. if (alevel <= mpeg1_max_level[0][run]){
  881. code= mpeg1_index_run[0][run] + alevel - 1;
  882. /* store the vlc & sign at once */
  883. put_bits(&s->pb, table_vlc[code][1]+1, (table_vlc[code][0]<<1) + sign);
  884. } else {
  885. /* escape seems to be pretty rare <5% so I do not optimize it */
  886. put_bits(&s->pb, table_vlc[111][1], table_vlc[111][0]);
  887. /* escape: only clip in this case */
  888. put_bits(&s->pb, 6, run);
  889. if(s->codec_id == CODEC_ID_MPEG1VIDEO){
  890. if (alevel < 128) {
  891. put_bits(&s->pb, 8, level & 0xff);
  892. } else {
  893. if (level < 0) {
  894. put_bits(&s->pb, 16, 0x8001 + level + 255);
  895. } else {
  896. put_bits(&s->pb, 16, level & 0xffff);
  897. }
  898. }
  899. }else{
  900. put_bits(&s->pb, 12, level & 0xfff);
  901. }
  902. }
  903. last_non_zero = i;
  904. }
  905. }
  906. /* end of block */
  907. put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
  908. }
  909. #endif //CONFIG_ENCODERS
  910. /******************************************/
  911. /* decoding */
  912. static VLC dc_lum_vlc;
  913. static VLC dc_chroma_vlc;
  914. static VLC mv_vlc;
  915. static VLC mbincr_vlc;
  916. static VLC mb_ptype_vlc;
  917. static VLC mb_btype_vlc;
  918. static VLC mb_pat_vlc;
  919. static void init_vlcs(void)
  920. {
  921. static int done = 0;
  922. if (!done) {
  923. done = 1;
  924. init_vlc(&dc_lum_vlc, DC_VLC_BITS, 12,
  925. vlc_dc_lum_bits, 1, 1,
  926. vlc_dc_lum_code, 2, 2, 1);
  927. init_vlc(&dc_chroma_vlc, DC_VLC_BITS, 12,
  928. vlc_dc_chroma_bits, 1, 1,
  929. vlc_dc_chroma_code, 2, 2, 1);
  930. init_vlc(&mv_vlc, MV_VLC_BITS, 17,
  931. &mbMotionVectorTable[0][1], 2, 1,
  932. &mbMotionVectorTable[0][0], 2, 1, 1);
  933. init_vlc(&mbincr_vlc, MBINCR_VLC_BITS, 36,
  934. &mbAddrIncrTable[0][1], 2, 1,
  935. &mbAddrIncrTable[0][0], 2, 1, 1);
  936. init_vlc(&mb_pat_vlc, MB_PAT_VLC_BITS, 64,
  937. &mbPatTable[0][1], 2, 1,
  938. &mbPatTable[0][0], 2, 1, 1);
  939. init_vlc(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
  940. &table_mb_ptype[0][1], 2, 1,
  941. &table_mb_ptype[0][0], 2, 1, 1);
  942. init_vlc(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,
  943. &table_mb_btype[0][1], 2, 1,
  944. &table_mb_btype[0][0], 2, 1, 1);
  945. init_rl(&rl_mpeg1, static_rl_table_store[0]);
  946. init_rl(&rl_mpeg2, static_rl_table_store[1]);
  947. init_2d_vlc_rl(&rl_mpeg1, 1);
  948. init_2d_vlc_rl(&rl_mpeg2, 1);
  949. }
  950. }
  951. static inline int get_dmv(MpegEncContext *s)
  952. {
  953. if(get_bits1(&s->gb))
  954. return 1 - (get_bits1(&s->gb) << 1);
  955. else
  956. return 0;
  957. }
  958. static inline int get_qscale(MpegEncContext *s)
  959. {
  960. int qscale = get_bits(&s->gb, 5);
  961. if (s->q_scale_type) {
  962. return non_linear_qscale[qscale];
  963. } else {
  964. return qscale << 1;
  965. }
  966. }
  967. /* motion type (for mpeg2) */
  968. #define MT_FIELD 1
  969. #define MT_FRAME 2
  970. #define MT_16X8 2
  971. #define MT_DMV 3
  972. static int mpeg_decode_mb(MpegEncContext *s,
  973. DCTELEM block[12][64])
  974. {
  975. int i, j, k, cbp, val, mb_type, motion_type;
  976. const int mb_block_count = 4 + (1<< s->chroma_format);
  977. dprintf(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
  978. assert(s->mb_skipped==0);
  979. if (s->mb_skip_run-- != 0) {
  980. if(s->pict_type == I_TYPE){
  981. av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
  982. return -1;
  983. }
  984. /* skip mb */
  985. s->mb_intra = 0;
  986. for(i=0;i<12;i++)
  987. s->block_last_index[i] = -1;
  988. if(s->picture_structure == PICT_FRAME)
  989. s->mv_type = MV_TYPE_16X16;
  990. else
  991. s->mv_type = MV_TYPE_FIELD;
  992. if (s->pict_type == P_TYPE) {
  993. /* if P type, zero motion vector is implied */
  994. s->mv_dir = MV_DIR_FORWARD;
  995. s->mv[0][0][0] = s->mv[0][0][1] = 0;
  996. s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
  997. s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
  998. s->field_select[0][0]= s->picture_structure - 1;
  999. s->mb_skipped = 1;
  1000. s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
  1001. } else {
  1002. int mb_type;
  1003. if(s->mb_x)
  1004. mb_type= s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1];
  1005. else
  1006. mb_type= s->current_picture.mb_type[ s->mb_width + (s->mb_y-1)*s->mb_stride - 1]; // FIXME not sure if this is allowed in mpeg at all,
  1007. if(IS_INTRA(mb_type))
  1008. return -1;
  1009. /* if B type, reuse previous vectors and directions */
  1010. s->mv[0][0][0] = s->last_mv[0][0][0];
  1011. s->mv[0][0][1] = s->last_mv[0][0][1];
  1012. s->mv[1][0][0] = s->last_mv[1][0][0];
  1013. s->mv[1][0][1] = s->last_mv[1][0][1];
  1014. s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]=
  1015. mb_type | MB_TYPE_SKIP;
  1016. // assert(s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1]&(MB_TYPE_16x16|MB_TYPE_16x8));
  1017. if((s->mv[0][0][0]|s->mv[0][0][1]|s->mv[1][0][0]|s->mv[1][0][1])==0)
  1018. s->mb_skipped = 1;
  1019. }
  1020. return 0;
  1021. }
  1022. switch(s->pict_type) {
  1023. default:
  1024. case I_TYPE:
  1025. if (get_bits1(&s->gb) == 0) {
  1026. if (get_bits1(&s->gb) == 0){
  1027. av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
  1028. return -1;
  1029. }
  1030. mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
  1031. } else {
  1032. mb_type = MB_TYPE_INTRA;
  1033. }
  1034. break;
  1035. case P_TYPE:
  1036. mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
  1037. if (mb_type < 0){
  1038. av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
  1039. return -1;
  1040. }
  1041. mb_type = ptype2mb_type[ mb_type ];
  1042. break;
  1043. case B_TYPE:
  1044. mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
  1045. if (mb_type < 0){
  1046. av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
  1047. return -1;
  1048. }
  1049. mb_type = btype2mb_type[ mb_type ];
  1050. break;
  1051. }
  1052. dprintf(s->avctx, "mb_type=%x\n", mb_type);
  1053. // motion_type = 0; /* avoid warning */
  1054. if (IS_INTRA(mb_type)) {
  1055. s->dsp.clear_blocks(s->block[0]);
  1056. if(!s->chroma_y_shift){
  1057. s->dsp.clear_blocks(s->block[6]);
  1058. }
  1059. /* compute dct type */
  1060. if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
  1061. !s->frame_pred_frame_dct) {
  1062. s->interlaced_dct = get_bits1(&s->gb);
  1063. }
  1064. if (IS_QUANT(mb_type))
  1065. s->qscale = get_qscale(s);
  1066. if (s->concealment_motion_vectors) {
  1067. /* just parse them */
  1068. if (s->picture_structure != PICT_FRAME)
  1069. skip_bits1(&s->gb); /* field select */
  1070. s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
  1071. mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
  1072. s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
  1073. mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
  1074. skip_bits1(&s->gb); /* marker */
  1075. }else
  1076. memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
  1077. s->mb_intra = 1;
  1078. #ifdef HAVE_XVMC
  1079. //one 1 we memcpy blocks in xvmcvideo
  1080. if(s->avctx->xvmc_acceleration > 1){
  1081. XVMC_pack_pblocks(s,-1);//inter are always full blocks
  1082. if(s->swap_uv){
  1083. exchange_uv(s);
  1084. }
  1085. }
  1086. #endif
  1087. if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
  1088. if(s->flags2 & CODEC_FLAG2_FAST){
  1089. for(i=0;i<6;i++) {
  1090. mpeg2_fast_decode_block_intra(s, s->pblocks[i], i);
  1091. }
  1092. }else{
  1093. for(i=0;i<mb_block_count;i++) {
  1094. if (mpeg2_decode_block_intra(s, s->pblocks[i], i) < 0)
  1095. return -1;
  1096. }
  1097. }
  1098. } else {
  1099. for(i=0;i<6;i++) {
  1100. if (mpeg1_decode_block_intra(s, s->pblocks[i], i) < 0)
  1101. return -1;
  1102. }
  1103. }
  1104. } else {
  1105. if (mb_type & MB_TYPE_ZERO_MV){
  1106. assert(mb_type & MB_TYPE_CBP);
  1107. /* compute dct type */
  1108. if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
  1109. !s->frame_pred_frame_dct) {
  1110. s->interlaced_dct = get_bits1(&s->gb);
  1111. }
  1112. if (IS_QUANT(mb_type))
  1113. s->qscale = get_qscale(s);
  1114. s->mv_dir = MV_DIR_FORWARD;
  1115. if(s->picture_structure == PICT_FRAME)
  1116. s->mv_type = MV_TYPE_16X16;
  1117. else{
  1118. s->mv_type = MV_TYPE_FIELD;
  1119. mb_type |= MB_TYPE_INTERLACED;
  1120. s->field_select[0][0]= s->picture_structure - 1;
  1121. }
  1122. s->last_mv[0][0][0] = 0;
  1123. s->last_mv[0][0][1] = 0;
  1124. s->last_mv[0][1][0] = 0;
  1125. s->last_mv[0][1][1] = 0;
  1126. s->mv[0][0][0] = 0;
  1127. s->mv[0][0][1] = 0;
  1128. }else{
  1129. assert(mb_type & MB_TYPE_L0L1);
  1130. //FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
  1131. /* get additional motion vector type */
  1132. if (s->frame_pred_frame_dct)
  1133. motion_type = MT_FRAME;
  1134. else{
  1135. motion_type = get_bits(&s->gb, 2);
  1136. }
  1137. /* compute dct type */
  1138. if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
  1139. !s->frame_pred_frame_dct && HAS_CBP(mb_type)) {
  1140. s->interlaced_dct = get_bits1(&s->gb);
  1141. }
  1142. if (IS_QUANT(mb_type))
  1143. s->qscale = get_qscale(s);
  1144. /* motion vectors */
  1145. s->mv_dir = 0;
  1146. for(i=0;i<2;i++) {
  1147. if (USES_LIST(mb_type, i)) {
  1148. s->mv_dir |= (MV_DIR_FORWARD >> i);
  1149. dprintf(s->avctx, "motion_type=%d\n", motion_type);
  1150. switch(motion_type) {
  1151. case MT_FRAME: /* or MT_16X8 */
  1152. if (s->picture_structure == PICT_FRAME) {
  1153. /* MT_FRAME */
  1154. mb_type |= MB_TYPE_16x16;
  1155. s->mv_type = MV_TYPE_16X16;
  1156. s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] =
  1157. mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
  1158. s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] =
  1159. mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
  1160. /* full_pel: only for mpeg1 */
  1161. if (s->full_pel[i]){
  1162. s->mv[i][0][0] <<= 1;
  1163. s->mv[i][0][1] <<= 1;
  1164. }
  1165. } else {
  1166. /* MT_16X8 */
  1167. mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
  1168. s->mv_type = MV_TYPE_16X8;
  1169. for(j=0;j<2;j++) {
  1170. s->field_select[i][j] = get_bits1(&s->gb);
  1171. for(k=0;k<2;k++) {
  1172. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  1173. s->last_mv[i][j][k]);
  1174. s->last_mv[i][j][k] = val;
  1175. s->mv[i][j][k] = val;
  1176. }
  1177. }
  1178. }
  1179. break;
  1180. case MT_FIELD:
  1181. s->mv_type = MV_TYPE_FIELD;
  1182. if (s->picture_structure == PICT_FRAME) {
  1183. mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
  1184. for(j=0;j<2;j++) {
  1185. s->field_select[i][j] = get_bits1(&s->gb);
  1186. val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  1187. s->last_mv[i][j][0]);
  1188. s->last_mv[i][j][0] = val;
  1189. s->mv[i][j][0] = val;
  1190. dprintf(s->avctx, "fmx=%d\n", val);
  1191. val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  1192. s->last_mv[i][j][1] >> 1);
  1193. s->last_mv[i][j][1] = val << 1;
  1194. s->mv[i][j][1] = val;
  1195. dprintf(s->avctx, "fmy=%d\n", val);
  1196. }
  1197. } else {
  1198. mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
  1199. s->field_select[i][0] = get_bits1(&s->gb);
  1200. for(k=0;k<2;k++) {
  1201. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  1202. s->last_mv[i][0][k]);
  1203. s->last_mv[i][0][k] = val;
  1204. s->last_mv[i][1][k] = val;
  1205. s->mv[i][0][k] = val;
  1206. }
  1207. }
  1208. break;
  1209. case MT_DMV:
  1210. {
  1211. int dmx, dmy, mx, my, m;
  1212. mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  1213. s->last_mv[i][0][0]);
  1214. s->last_mv[i][0][0] = mx;
  1215. s->last_mv[i][1][0] = mx;
  1216. dmx = get_dmv(s);
  1217. my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  1218. s->last_mv[i][0][1] >> 1);
  1219. dmy = get_dmv(s);
  1220. s->mv_type = MV_TYPE_DMV;
  1221. s->last_mv[i][0][1] = my<<1;
  1222. s->last_mv[i][1][1] = my<<1;
  1223. s->mv[i][0][0] = mx;
  1224. s->mv[i][0][1] = my;
  1225. s->mv[i][1][0] = mx;//not used
  1226. s->mv[i][1][1] = my;//not used
  1227. if (s->picture_structure == PICT_FRAME) {
  1228. mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
  1229. //m = 1 + 2 * s->top_field_first;
  1230. m = s->top_field_first ? 1 : 3;
  1231. /* top -> top pred */
  1232. s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  1233. s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
  1234. m = 4 - m;
  1235. s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  1236. s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
  1237. } else {
  1238. mb_type |= MB_TYPE_16x16;
  1239. s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
  1240. s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
  1241. if(s->picture_structure == PICT_TOP_FIELD)
  1242. s->mv[i][2][1]--;
  1243. else
  1244. s->mv[i][2][1]++;
  1245. }
  1246. }
  1247. break;
  1248. default:
  1249. av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
  1250. return -1;
  1251. }
  1252. }
  1253. }
  1254. }
  1255. s->mb_intra = 0;
  1256. if (HAS_CBP(mb_type)) {
  1257. s->dsp.clear_blocks(s->block[0]);
  1258. if(!s->chroma_y_shift){
  1259. s->dsp.clear_blocks(s->block[6]);
  1260. }
  1261. cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
  1262. if (cbp < 0 || ((cbp == 0) && (s->chroma_format < 2)) ){
  1263. av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
  1264. return -1;
  1265. }
  1266. if(mb_block_count > 6){
  1267. cbp<<= mb_block_count-6;
  1268. cbp |= get_bits(&s->gb, mb_block_count-6);
  1269. }
  1270. #ifdef HAVE_XVMC
  1271. //on 1 we memcpy blocks in xvmcvideo
  1272. if(s->avctx->xvmc_acceleration > 1){
  1273. XVMC_pack_pblocks(s,cbp);
  1274. if(s->swap_uv){
  1275. exchange_uv(s);
  1276. }
  1277. }
  1278. #endif
  1279. if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
  1280. if(s->flags2 & CODEC_FLAG2_FAST){
  1281. for(i=0;i<6;i++) {
  1282. if(cbp & 32) {
  1283. mpeg2_fast_decode_block_non_intra(s, s->pblocks[i], i);
  1284. } else {
  1285. s->block_last_index[i] = -1;
  1286. }
  1287. cbp+=cbp;
  1288. }
  1289. }else{
  1290. cbp<<= 12-mb_block_count;
  1291. for(i=0;i<mb_block_count;i++) {
  1292. if ( cbp & (1<<11) ) {
  1293. if (mpeg2_decode_block_non_intra(s, s->pblocks[i], i) < 0)
  1294. return -1;
  1295. } else {
  1296. s->block_last_index[i] = -1;
  1297. }
  1298. cbp+=cbp;
  1299. }
  1300. }
  1301. } else {
  1302. if(s->flags2 & CODEC_FLAG2_FAST){
  1303. for(i=0;i<6;i++) {
  1304. if (cbp & 32) {
  1305. mpeg1_fast_decode_block_inter(s, s->pblocks[i], i);
  1306. } else {
  1307. s->block_last_index[i] = -1;
  1308. }
  1309. cbp+=cbp;
  1310. }
  1311. }else{
  1312. for(i=0;i<6;i++) {
  1313. if (cbp & 32) {
  1314. if (mpeg1_decode_block_inter(s, s->pblocks[i], i) < 0)
  1315. return -1;
  1316. } else {
  1317. s->block_last_index[i] = -1;
  1318. }
  1319. cbp+=cbp;
  1320. }
  1321. }
  1322. }
  1323. }else{
  1324. for(i=0;i<12;i++)
  1325. s->block_last_index[i] = -1;
  1326. }
  1327. }
  1328. s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= mb_type;
  1329. return 0;
  1330. }
  1331. /* as h263, but only 17 codes */
  1332. static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
  1333. {
  1334. int code, sign, val, l, shift;
  1335. code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
  1336. if (code == 0) {
  1337. return pred;
  1338. }
  1339. if (code < 0) {
  1340. return 0xffff;
  1341. }
  1342. sign = get_bits1(&s->gb);
  1343. shift = fcode - 1;
  1344. val = code;
  1345. if (shift) {
  1346. val = (val - 1) << shift;
  1347. val |= get_bits(&s->gb, shift);
  1348. val++;
  1349. }
  1350. if (sign)
  1351. val = -val;
  1352. val += pred;
  1353. /* modulo decoding */
  1354. l= INT_BIT - 5 - shift;
  1355. val = (val<<l)>>l;
  1356. return val;
  1357. }
  1358. static inline int decode_dc(GetBitContext *gb, int component)
  1359. {
  1360. int code, diff;
  1361. if (component == 0) {
  1362. code = get_vlc2(gb, dc_lum_vlc.table, DC_VLC_BITS, 2);
  1363. } else {
  1364. code = get_vlc2(gb, dc_chroma_vlc.table, DC_VLC_BITS, 2);
  1365. }
  1366. if (code < 0){
  1367. av_log(NULL, AV_LOG_ERROR, "invalid dc code at\n");
  1368. return 0xffff;
  1369. }
  1370. if (code == 0) {
  1371. diff = 0;
  1372. } else {
  1373. diff = get_xbits(gb, code);
  1374. }
  1375. return diff;
  1376. }
  1377. static inline int mpeg1_decode_block_intra(MpegEncContext *s,
  1378. DCTELEM *block,
  1379. int n)
  1380. {
  1381. int level, dc, diff, i, j, run;
  1382. int component;
  1383. RLTable *rl = &rl_mpeg1;
  1384. uint8_t * const scantable= s->intra_scantable.permutated;
  1385. const uint16_t *quant_matrix= s->intra_matrix;
  1386. const int qscale= s->qscale;
  1387. /* DC coef */
  1388. component = (n <= 3 ? 0 : n - 4 + 1);
  1389. diff = decode_dc(&s->gb, component);
  1390. if (diff >= 0xffff)
  1391. return -1;
  1392. dc = s->last_dc[component];
  1393. dc += diff;
  1394. s->last_dc[component] = dc;
  1395. block[0] = dc<<3;
  1396. dprintf(s->avctx, "dc=%d diff=%d\n", dc, diff);
  1397. i = 0;
  1398. {
  1399. OPEN_READER(re, &s->gb);
  1400. /* now quantify & encode AC coefs */
  1401. for(;;) {
  1402. UPDATE_CACHE(re, &s->gb);
  1403. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  1404. if(level == 127){
  1405. break;
  1406. } else if(level != 0) {
  1407. i += run;
  1408. j = scantable[i];
  1409. level= (level*qscale*quant_matrix[j])>>4;
  1410. level= (level-1)|1;
  1411. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1412. LAST_SKIP_BITS(re, &s->gb, 1);
  1413. } else {
  1414. /* escape */
  1415. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1416. UPDATE_CACHE(re, &s->gb);
  1417. level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
  1418. if (level == -128) {
  1419. level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
  1420. } else if (level == 0) {
  1421. level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
  1422. }
  1423. i += run;
  1424. j = scantable[i];
  1425. if(level<0){
  1426. level= -level;
  1427. level= (level*qscale*quant_matrix[j])>>4;
  1428. level= (level-1)|1;
  1429. level= -level;
  1430. }else{
  1431. level= (level*qscale*quant_matrix[j])>>4;
  1432. level= (level-1)|1;
  1433. }
  1434. }
  1435. if (i > 63){
  1436. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1437. return -1;
  1438. }
  1439. block[j] = level;
  1440. }
  1441. CLOSE_READER(re, &s->gb);
  1442. }
  1443. s->block_last_index[n] = i;
  1444. return 0;
  1445. }
  1446. static inline int mpeg1_decode_block_inter(MpegEncContext *s,
  1447. DCTELEM *block,
  1448. int n)
  1449. {
  1450. int level, i, j, run;
  1451. RLTable *rl = &rl_mpeg1;
  1452. uint8_t * const scantable= s->intra_scantable.permutated;
  1453. const uint16_t *quant_matrix= s->inter_matrix;
  1454. const int qscale= s->qscale;
  1455. {
  1456. OPEN_READER(re, &s->gb);
  1457. i = -1;
  1458. /* special case for the first coef. no need to add a second vlc table */
  1459. UPDATE_CACHE(re, &s->gb);
  1460. if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
  1461. level= (3*qscale*quant_matrix[0])>>5;
  1462. level= (level-1)|1;
  1463. if(GET_CACHE(re, &s->gb)&0x40000000)
  1464. level= -level;
  1465. block[0] = level;
  1466. i++;
  1467. SKIP_BITS(re, &s->gb, 2);
  1468. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  1469. goto end;
  1470. }
  1471. /* now quantify & encode AC coefs */
  1472. for(;;) {
  1473. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  1474. if(level != 0) {
  1475. i += run;
  1476. j = scantable[i];
  1477. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  1478. level= (level-1)|1;
  1479. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1480. SKIP_BITS(re, &s->gb, 1);
  1481. } else {
  1482. /* escape */
  1483. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1484. UPDATE_CACHE(re, &s->gb);
  1485. level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
  1486. if (level == -128) {
  1487. level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
  1488. } else if (level == 0) {
  1489. level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
  1490. }
  1491. i += run;
  1492. j = scantable[i];
  1493. if(level<0){
  1494. level= -level;
  1495. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  1496. level= (level-1)|1;
  1497. level= -level;
  1498. }else{
  1499. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  1500. level= (level-1)|1;
  1501. }
  1502. }
  1503. if (i > 63){
  1504. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1505. return -1;
  1506. }
  1507. block[j] = level;
  1508. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  1509. break;
  1510. UPDATE_CACHE(re, &s->gb);
  1511. }
  1512. end:
  1513. LAST_SKIP_BITS(re, &s->gb, 2);
  1514. CLOSE_READER(re, &s->gb);
  1515. }
  1516. s->block_last_index[n] = i;
  1517. return 0;
  1518. }
  1519. static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
  1520. {
  1521. int level, i, j, run;
  1522. RLTable *rl = &rl_mpeg1;
  1523. uint8_t * const scantable= s->intra_scantable.permutated;
  1524. const int qscale= s->qscale;
  1525. {
  1526. OPEN_READER(re, &s->gb);
  1527. i = -1;
  1528. /* special case for the first coef. no need to add a second vlc table */
  1529. UPDATE_CACHE(re, &s->gb);
  1530. if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
  1531. level= (3*qscale)>>1;
  1532. level= (level-1)|1;
  1533. if(GET_CACHE(re, &s->gb)&0x40000000)
  1534. level= -level;
  1535. block[0] = level;
  1536. i++;
  1537. SKIP_BITS(re, &s->gb, 2);
  1538. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  1539. goto end;
  1540. }
  1541. /* now quantify & encode AC coefs */
  1542. for(;;) {
  1543. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  1544. if(level != 0) {
  1545. i += run;
  1546. j = scantable[i];
  1547. level= ((level*2+1)*qscale)>>1;
  1548. level= (level-1)|1;
  1549. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1550. SKIP_BITS(re, &s->gb, 1);
  1551. } else {
  1552. /* escape */
  1553. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1554. UPDATE_CACHE(re, &s->gb);
  1555. level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
  1556. if (level == -128) {
  1557. level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
  1558. } else if (level == 0) {
  1559. level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
  1560. }
  1561. i += run;
  1562. j = scantable[i];
  1563. if(level<0){
  1564. level= -level;
  1565. level= ((level*2+1)*qscale)>>1;
  1566. level= (level-1)|1;
  1567. level= -level;
  1568. }else{
  1569. level= ((level*2+1)*qscale)>>1;
  1570. level= (level-1)|1;
  1571. }
  1572. }
  1573. block[j] = level;
  1574. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  1575. break;
  1576. UPDATE_CACHE(re, &s->gb);
  1577. }
  1578. end:
  1579. LAST_SKIP_BITS(re, &s->gb, 2);
  1580. CLOSE_READER(re, &s->gb);
  1581. }
  1582. s->block_last_index[n] = i;
  1583. return 0;
  1584. }
  1585. static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
  1586. DCTELEM *block,
  1587. int n)
  1588. {
  1589. int level, i, j, run;
  1590. RLTable *rl = &rl_mpeg1;
  1591. uint8_t * const scantable= s->intra_scantable.permutated;
  1592. const uint16_t *quant_matrix;
  1593. const int qscale= s->qscale;
  1594. int mismatch;
  1595. mismatch = 1;
  1596. {
  1597. OPEN_READER(re, &s->gb);
  1598. i = -1;
  1599. if (n < 4)
  1600. quant_matrix = s->inter_matrix;
  1601. else
  1602. quant_matrix = s->chroma_inter_matrix;
  1603. /* special case for the first coef. no need to add a second vlc table */
  1604. UPDATE_CACHE(re, &s->gb);
  1605. if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
  1606. level= (3*qscale*quant_matrix[0])>>5;
  1607. if(GET_CACHE(re, &s->gb)&0x40000000)
  1608. level= -level;
  1609. block[0] = level;
  1610. mismatch ^= level;
  1611. i++;
  1612. SKIP_BITS(re, &s->gb, 2);
  1613. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  1614. goto end;
  1615. }
  1616. /* now quantify & encode AC coefs */
  1617. for(;;) {
  1618. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  1619. if(level != 0) {
  1620. i += run;
  1621. j = scantable[i];
  1622. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  1623. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1624. SKIP_BITS(re, &s->gb, 1);
  1625. } else {
  1626. /* escape */
  1627. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1628. UPDATE_CACHE(re, &s->gb);
  1629. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  1630. i += run;
  1631. j = scantable[i];
  1632. if(level<0){
  1633. level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
  1634. level= -level;
  1635. }else{
  1636. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  1637. }
  1638. }
  1639. if (i > 63){
  1640. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1641. return -1;
  1642. }
  1643. mismatch ^= level;
  1644. block[j] = level;
  1645. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  1646. break;
  1647. UPDATE_CACHE(re, &s->gb);
  1648. }
  1649. end:
  1650. LAST_SKIP_BITS(re, &s->gb, 2);
  1651. CLOSE_READER(re, &s->gb);
  1652. }
  1653. block[63] ^= (mismatch & 1);
  1654. s->block_last_index[n] = i;
  1655. return 0;
  1656. }
  1657. static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
  1658. DCTELEM *block,
  1659. int n)
  1660. {
  1661. int level, i, j, run;
  1662. RLTable *rl = &rl_mpeg1;
  1663. uint8_t * const scantable= s->intra_scantable.permutated;
  1664. const int qscale= s->qscale;
  1665. OPEN_READER(re, &s->gb);
  1666. i = -1;
  1667. /* special case for the first coef. no need to add a second vlc table */
  1668. UPDATE_CACHE(re, &s->gb);
  1669. if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
  1670. level= (3*qscale)>>1;
  1671. if(GET_CACHE(re, &s->gb)&0x40000000)
  1672. level= -level;
  1673. block[0] = level;
  1674. i++;
  1675. SKIP_BITS(re, &s->gb, 2);
  1676. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  1677. goto end;
  1678. }
  1679. /* now quantify & encode AC coefs */
  1680. for(;;) {
  1681. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  1682. if(level != 0) {
  1683. i += run;
  1684. j = scantable[i];
  1685. level= ((level*2+1)*qscale)>>1;
  1686. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1687. SKIP_BITS(re, &s->gb, 1);
  1688. } else {
  1689. /* escape */
  1690. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1691. UPDATE_CACHE(re, &s->gb);
  1692. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  1693. i += run;
  1694. j = scantable[i];
  1695. if(level<0){
  1696. level= ((-level*2+1)*qscale)>>1;
  1697. level= -level;
  1698. }else{
  1699. level= ((level*2+1)*qscale)>>1;
  1700. }
  1701. }
  1702. block[j] = level;
  1703. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  1704. break;
  1705. UPDATE_CACHE(re, &s->gb);
  1706. }
  1707. end:
  1708. LAST_SKIP_BITS(re, &s->gb, 2);
  1709. CLOSE_READER(re, &s->gb);
  1710. s->block_last_index[n] = i;
  1711. return 0;
  1712. }
  1713. static inline int mpeg2_decode_block_intra(MpegEncContext *s,
  1714. DCTELEM *block,
  1715. int n)
  1716. {
  1717. int level, dc, diff, i, j, run;
  1718. int component;
  1719. RLTable *rl;
  1720. uint8_t * const scantable= s->intra_scantable.permutated;
  1721. const uint16_t *quant_matrix;
  1722. const int qscale= s->qscale;
  1723. int mismatch;
  1724. /* DC coef */
  1725. if (n < 4){
  1726. quant_matrix = s->intra_matrix;
  1727. component = 0;
  1728. }else{
  1729. quant_matrix = s->chroma_intra_matrix;
  1730. component = (n&1) + 1;
  1731. }
  1732. diff = decode_dc(&s->gb, component);
  1733. if (diff >= 0xffff)
  1734. return -1;
  1735. dc = s->last_dc[component];
  1736. dc += diff;
  1737. s->last_dc[component] = dc;
  1738. block[0] = dc << (3 - s->intra_dc_precision);
  1739. dprintf(s->avctx, "dc=%d\n", block[0]);
  1740. mismatch = block[0] ^ 1;
  1741. i = 0;
  1742. if (s->intra_vlc_format)
  1743. rl = &rl_mpeg2;
  1744. else
  1745. rl = &rl_mpeg1;
  1746. {
  1747. OPEN_READER(re, &s->gb);
  1748. /* now quantify & encode AC coefs */
  1749. for(;;) {
  1750. UPDATE_CACHE(re, &s->gb);
  1751. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  1752. if(level == 127){
  1753. break;
  1754. } else if(level != 0) {
  1755. i += run;
  1756. j = scantable[i];
  1757. level= (level*qscale*quant_matrix[j])>>4;
  1758. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1759. LAST_SKIP_BITS(re, &s->gb, 1);
  1760. } else {
  1761. /* escape */
  1762. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1763. UPDATE_CACHE(re, &s->gb);
  1764. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  1765. i += run;
  1766. j = scantable[i];
  1767. if(level<0){
  1768. level= (-level*qscale*quant_matrix[j])>>4;
  1769. level= -level;
  1770. }else{
  1771. level= (level*qscale*quant_matrix[j])>>4;
  1772. }
  1773. }
  1774. if (i > 63){
  1775. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1776. return -1;
  1777. }
  1778. mismatch^= level;
  1779. block[j] = level;
  1780. }
  1781. CLOSE_READER(re, &s->gb);
  1782. }
  1783. block[63]^= mismatch&1;
  1784. s->block_last_index[n] = i;
  1785. return 0;
  1786. }
  1787. static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
  1788. DCTELEM *block,
  1789. int n)
  1790. {
  1791. int level, dc, diff, j, run;
  1792. int component;
  1793. RLTable *rl;
  1794. uint8_t * scantable= s->intra_scantable.permutated;
  1795. const uint16_t *quant_matrix;
  1796. const int qscale= s->qscale;
  1797. /* DC coef */
  1798. if (n < 4){
  1799. quant_matrix = s->intra_matrix;
  1800. component = 0;
  1801. }else{
  1802. quant_matrix = s->chroma_intra_matrix;
  1803. component = (n&1) + 1;
  1804. }
  1805. diff = decode_dc(&s->gb, component);
  1806. if (diff >= 0xffff)
  1807. return -1;
  1808. dc = s->last_dc[component];
  1809. dc += diff;
  1810. s->last_dc[component] = dc;
  1811. block[0] = dc << (3 - s->intra_dc_precision);
  1812. if (s->intra_vlc_format)
  1813. rl = &rl_mpeg2;
  1814. else
  1815. rl = &rl_mpeg1;
  1816. {
  1817. OPEN_READER(re, &s->gb);
  1818. /* now quantify & encode AC coefs */
  1819. for(;;) {
  1820. UPDATE_CACHE(re, &s->gb);
  1821. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  1822. if(level == 127){
  1823. break;
  1824. } else if(level != 0) {
  1825. scantable += run;
  1826. j = *scantable;
  1827. level= (level*qscale*quant_matrix[j])>>4;
  1828. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1829. LAST_SKIP_BITS(re, &s->gb, 1);
  1830. } else {
  1831. /* escape */
  1832. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1833. UPDATE_CACHE(re, &s->gb);
  1834. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  1835. scantable += run;
  1836. j = *scantable;
  1837. if(level<0){
  1838. level= (-level*qscale*quant_matrix[j])>>4;
  1839. level= -level;
  1840. }else{
  1841. level= (level*qscale*quant_matrix[j])>>4;
  1842. }
  1843. }
  1844. block[j] = level;
  1845. }
  1846. CLOSE_READER(re, &s->gb);
  1847. }
  1848. s->block_last_index[n] = scantable - s->intra_scantable.permutated;
  1849. return 0;
  1850. }
  1851. typedef struct Mpeg1Context {
  1852. MpegEncContext mpeg_enc_ctx;
  1853. int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
  1854. int repeat_field; /* true if we must repeat the field */
  1855. AVPanScan pan_scan; /** some temporary storage for the panscan */
  1856. int slice_count;
  1857. int swap_uv;//indicate VCR2
  1858. int save_aspect_info;
  1859. int save_width, save_height;
  1860. AVRational frame_rate_ext; ///< MPEG-2 specific framerate modificator
  1861. } Mpeg1Context;
  1862. static int mpeg_decode_init(AVCodecContext *avctx)
  1863. {
  1864. Mpeg1Context *s = avctx->priv_data;
  1865. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  1866. int i;
  1867. //we need some parmutation to store
  1868. //matrixes, until MPV_common_init()
  1869. //set the real permutatuon
  1870. for(i=0;i<64;i++)
  1871. s2->dsp.idct_permutation[i]=i;
  1872. MPV_decode_defaults(s2);
  1873. s->mpeg_enc_ctx.avctx= avctx;
  1874. s->mpeg_enc_ctx.flags= avctx->flags;
  1875. s->mpeg_enc_ctx.flags2= avctx->flags2;
  1876. common_init(&s->mpeg_enc_ctx);
  1877. init_vlcs();
  1878. s->mpeg_enc_ctx_allocated = 0;
  1879. s->mpeg_enc_ctx.picture_number = 0;
  1880. s->repeat_field = 0;
  1881. s->mpeg_enc_ctx.codec_id= avctx->codec->id;
  1882. return 0;
  1883. }
  1884. static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
  1885. const uint8_t *new_perm){
  1886. uint16_t temp_matrix[64];
  1887. int i;
  1888. memcpy(temp_matrix,matrix,64*sizeof(uint16_t));
  1889. for(i=0;i<64;i++){
  1890. matrix[new_perm[i]] = temp_matrix[old_perm[i]];
  1891. }
  1892. }
  1893. //Call this function when we know all parameters
  1894. //it may be called in different places for mpeg1 and mpeg2
  1895. static int mpeg_decode_postinit(AVCodecContext *avctx){
  1896. Mpeg1Context *s1 = avctx->priv_data;
  1897. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1898. uint8_t old_permutation[64];
  1899. if (
  1900. (s1->mpeg_enc_ctx_allocated == 0)||
  1901. avctx->coded_width != s->width ||
  1902. avctx->coded_height != s->height||
  1903. s1->save_width != s->width ||
  1904. s1->save_height != s->height ||
  1905. s1->save_aspect_info != s->aspect_ratio_info||
  1906. 0)
  1907. {
  1908. if (s1->mpeg_enc_ctx_allocated) {
  1909. ParseContext pc= s->parse_context;
  1910. s->parse_context.buffer=0;
  1911. MPV_common_end(s);
  1912. s->parse_context= pc;
  1913. }
  1914. if( (s->width == 0 )||(s->height == 0))
  1915. return -2;
  1916. avcodec_set_dimensions(avctx, s->width, s->height);
  1917. avctx->bit_rate = s->bit_rate;
  1918. s1->save_aspect_info = s->aspect_ratio_info;
  1919. s1->save_width = s->width;
  1920. s1->save_height = s->height;
  1921. //low_delay may be forced, in this case we will have B frames
  1922. //that behave like P frames
  1923. avctx->has_b_frames = !(s->low_delay);
  1924. if(avctx->sub_id==1){//s->codec_id==avctx->codec_id==CODEC_ID
  1925. //mpeg1 fps
  1926. avctx->time_base.den= ff_frame_rate_tab[s->frame_rate_index].num;
  1927. avctx->time_base.num= ff_frame_rate_tab[s->frame_rate_index].den;
  1928. //mpeg1 aspect
  1929. avctx->sample_aspect_ratio= av_d2q(
  1930. 1.0/mpeg1_aspect[s->aspect_ratio_info], 255);
  1931. }else{//mpeg2
  1932. //mpeg2 fps
  1933. av_reduce(
  1934. &s->avctx->time_base.den,
  1935. &s->avctx->time_base.num,
  1936. ff_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num,
  1937. ff_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
  1938. 1<<30);
  1939. //mpeg2 aspect
  1940. if(s->aspect_ratio_info > 1){
  1941. if( (s1->pan_scan.width == 0 )||(s1->pan_scan.height == 0) ){
  1942. s->avctx->sample_aspect_ratio=
  1943. av_div_q(
  1944. mpeg2_aspect[s->aspect_ratio_info],
  1945. (AVRational){s->width, s->height}
  1946. );
  1947. }else{
  1948. s->avctx->sample_aspect_ratio=
  1949. av_div_q(
  1950. mpeg2_aspect[s->aspect_ratio_info],
  1951. (AVRational){s1->pan_scan.width, s1->pan_scan.height}
  1952. );
  1953. }
  1954. }else{
  1955. s->avctx->sample_aspect_ratio=
  1956. mpeg2_aspect[s->aspect_ratio_info];
  1957. }
  1958. }//mpeg2
  1959. if(avctx->xvmc_acceleration){
  1960. avctx->pix_fmt = avctx->get_format(avctx,pixfmt_xvmc_mpg2_420);
  1961. }else{
  1962. if(s->chroma_format < 2){
  1963. avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_420);
  1964. }else
  1965. if(s->chroma_format == 2){
  1966. avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_422);
  1967. }else
  1968. if(s->chroma_format > 2){
  1969. avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_444);
  1970. }
  1971. }
  1972. //until then pix_fmt may be changed right after codec init
  1973. if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT )
  1974. if( avctx->idct_algo == FF_IDCT_AUTO )
  1975. avctx->idct_algo = FF_IDCT_SIMPLE;
  1976. //quantization matrixes may need reordering
  1977. //if dct permutation is changed
  1978. memcpy(old_permutation,s->dsp.idct_permutation,64*sizeof(uint8_t));
  1979. if (MPV_common_init(s) < 0)
  1980. return -2;
  1981. quant_matrix_rebuild(s->intra_matrix, old_permutation,s->dsp.idct_permutation);
  1982. quant_matrix_rebuild(s->inter_matrix, old_permutation,s->dsp.idct_permutation);
  1983. quant_matrix_rebuild(s->chroma_intra_matrix,old_permutation,s->dsp.idct_permutation);
  1984. quant_matrix_rebuild(s->chroma_inter_matrix,old_permutation,s->dsp.idct_permutation);
  1985. s1->mpeg_enc_ctx_allocated = 1;
  1986. }
  1987. return 0;
  1988. }
  1989. static int mpeg1_decode_picture(AVCodecContext *avctx,
  1990. const uint8_t *buf, int buf_size)
  1991. {
  1992. Mpeg1Context *s1 = avctx->priv_data;
  1993. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1994. int ref, f_code, vbv_delay;
  1995. if(mpeg_decode_postinit(s->avctx) < 0)
  1996. return -2;
  1997. init_get_bits(&s->gb, buf, buf_size*8);
  1998. ref = get_bits(&s->gb, 10); /* temporal ref */
  1999. s->pict_type = get_bits(&s->gb, 3);
  2000. if(s->pict_type == 0 || s->pict_type > 3)
  2001. return -1;
  2002. vbv_delay= get_bits(&s->gb, 16);
  2003. if (s->pict_type == P_TYPE || s->pict_type == B_TYPE) {
  2004. s->full_pel[0] = get_bits1(&s->gb);
  2005. f_code = get_bits(&s->gb, 3);
  2006. if (f_code == 0 && avctx->error_resilience >= FF_ER_COMPLIANT)
  2007. return -1;
  2008. s->mpeg_f_code[0][0] = f_code;
  2009. s->mpeg_f_code[0][1] = f_code;
  2010. }
  2011. if (s->pict_type == B_TYPE) {
  2012. s->full_pel[1] = get_bits1(&s->gb);
  2013. f_code = get_bits(&s->gb, 3);
  2014. if (f_code == 0 && avctx->error_resilience >= FF_ER_COMPLIANT)
  2015. return -1;
  2016. s->mpeg_f_code[1][0] = f_code;
  2017. s->mpeg_f_code[1][1] = f_code;
  2018. }
  2019. s->current_picture.pict_type= s->pict_type;
  2020. s->current_picture.key_frame= s->pict_type == I_TYPE;
  2021. if(avctx->debug & FF_DEBUG_PICT_INFO)
  2022. av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
  2023. s->y_dc_scale = 8;
  2024. s->c_dc_scale = 8;
  2025. s->first_slice = 1;
  2026. return 0;
  2027. }
  2028. static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
  2029. {
  2030. MpegEncContext *s= &s1->mpeg_enc_ctx;
  2031. int horiz_size_ext, vert_size_ext;
  2032. int bit_rate_ext;
  2033. skip_bits(&s->gb, 1); /* profil and level esc*/
  2034. s->avctx->profile= get_bits(&s->gb, 3);
  2035. s->avctx->level= get_bits(&s->gb, 4);
  2036. s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
  2037. s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
  2038. horiz_size_ext = get_bits(&s->gb, 2);
  2039. vert_size_ext = get_bits(&s->gb, 2);
  2040. s->width |= (horiz_size_ext << 12);
  2041. s->height |= (vert_size_ext << 12);
  2042. bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
  2043. s->bit_rate += (bit_rate_ext << 18) * 400;
  2044. skip_bits1(&s->gb); /* marker */
  2045. s->avctx->rc_buffer_size += get_bits(&s->gb, 8)*1024*16<<10;
  2046. s->low_delay = get_bits1(&s->gb);
  2047. if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
  2048. s1->frame_rate_ext.num = get_bits(&s->gb, 2)+1;
  2049. s1->frame_rate_ext.den = get_bits(&s->gb, 5)+1;
  2050. dprintf(s->avctx, "sequence extension\n");
  2051. s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
  2052. s->avctx->sub_id = 2; /* indicates mpeg2 found */
  2053. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  2054. av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
  2055. s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
  2056. }
  2057. static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
  2058. {
  2059. MpegEncContext *s= &s1->mpeg_enc_ctx;
  2060. int color_description, w, h;
  2061. skip_bits(&s->gb, 3); /* video format */
  2062. color_description= get_bits1(&s->gb);
  2063. if(color_description){
  2064. skip_bits(&s->gb, 8); /* color primaries */
  2065. skip_bits(&s->gb, 8); /* transfer_characteristics */
  2066. skip_bits(&s->gb, 8); /* matrix_coefficients */
  2067. }
  2068. w= get_bits(&s->gb, 14);
  2069. skip_bits(&s->gb, 1); //marker
  2070. h= get_bits(&s->gb, 14);
  2071. skip_bits(&s->gb, 1); //marker
  2072. s1->pan_scan.width= 16*w;
  2073. s1->pan_scan.height=16*h;
  2074. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  2075. av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
  2076. }
  2077. static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
  2078. {
  2079. MpegEncContext *s= &s1->mpeg_enc_ctx;
  2080. int i,nofco;
  2081. nofco = 1;
  2082. if(s->progressive_sequence){
  2083. if(s->repeat_first_field){
  2084. nofco++;
  2085. if(s->top_field_first)
  2086. nofco++;
  2087. }
  2088. }else{
  2089. if(s->picture_structure == PICT_FRAME){
  2090. nofco++;
  2091. if(s->repeat_first_field)
  2092. nofco++;
  2093. }
  2094. }
  2095. for(i=0; i<nofco; i++){
  2096. s1->pan_scan.position[i][0]= get_sbits(&s->gb, 16);
  2097. skip_bits(&s->gb, 1); //marker
  2098. s1->pan_scan.position[i][1]= get_sbits(&s->gb, 16);
  2099. skip_bits(&s->gb, 1); //marker
  2100. }
  2101. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  2102. av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
  2103. s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
  2104. s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
  2105. s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]
  2106. );
  2107. }
  2108. static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
  2109. {
  2110. int i, v, j;
  2111. dprintf(s->avctx, "matrix extension\n");
  2112. if (get_bits1(&s->gb)) {
  2113. for(i=0;i<64;i++) {
  2114. v = get_bits(&s->gb, 8);
  2115. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  2116. s->intra_matrix[j] = v;
  2117. s->chroma_intra_matrix[j] = v;
  2118. }
  2119. }
  2120. if (get_bits1(&s->gb)) {
  2121. for(i=0;i<64;i++) {
  2122. v = get_bits(&s->gb, 8);
  2123. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  2124. s->inter_matrix[j] = v;
  2125. s->chroma_inter_matrix[j] = v;
  2126. }
  2127. }
  2128. if (get_bits1(&s->gb)) {
  2129. for(i=0;i<64;i++) {
  2130. v = get_bits(&s->gb, 8);
  2131. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  2132. s->chroma_intra_matrix[j] = v;
  2133. }
  2134. }
  2135. if (get_bits1(&s->gb)) {
  2136. for(i=0;i<64;i++) {
  2137. v = get_bits(&s->gb, 8);
  2138. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  2139. s->chroma_inter_matrix[j] = v;
  2140. }
  2141. }
  2142. }
  2143. static void mpeg_decode_picture_coding_extension(MpegEncContext *s)
  2144. {
  2145. s->full_pel[0] = s->full_pel[1] = 0;
  2146. s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
  2147. s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
  2148. s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
  2149. s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
  2150. s->intra_dc_precision = get_bits(&s->gb, 2);
  2151. s->picture_structure = get_bits(&s->gb, 2);
  2152. s->top_field_first = get_bits1(&s->gb);
  2153. s->frame_pred_frame_dct = get_bits1(&s->gb);
  2154. s->concealment_motion_vectors = get_bits1(&s->gb);
  2155. s->q_scale_type = get_bits1(&s->gb);
  2156. s->intra_vlc_format = get_bits1(&s->gb);
  2157. s->alternate_scan = get_bits1(&s->gb);
  2158. s->repeat_first_field = get_bits1(&s->gb);
  2159. s->chroma_420_type = get_bits1(&s->gb);
  2160. s->progressive_frame = get_bits1(&s->gb);
  2161. if(s->picture_structure == PICT_FRAME){
  2162. s->first_field=0;
  2163. s->v_edge_pos= 16*s->mb_height;
  2164. }else{
  2165. s->first_field ^= 1;
  2166. s->v_edge_pos= 8*s->mb_height;
  2167. memset(s->mbskip_table, 0, s->mb_stride*s->mb_height);
  2168. }
  2169. if(s->alternate_scan){
  2170. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
  2171. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
  2172. }else{
  2173. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
  2174. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
  2175. }
  2176. /* composite display not parsed */
  2177. dprintf(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
  2178. dprintf(s->avctx, "picture_structure=%d\n", s->picture_structure);
  2179. dprintf(s->avctx, "top field first=%d\n", s->top_field_first);
  2180. dprintf(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
  2181. dprintf(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
  2182. dprintf(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
  2183. dprintf(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
  2184. dprintf(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
  2185. dprintf(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
  2186. }
  2187. static void mpeg_decode_extension(AVCodecContext *avctx,
  2188. const uint8_t *buf, int buf_size)
  2189. {
  2190. Mpeg1Context *s1 = avctx->priv_data;
  2191. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2192. int ext_type;
  2193. init_get_bits(&s->gb, buf, buf_size*8);
  2194. ext_type = get_bits(&s->gb, 4);
  2195. switch(ext_type) {
  2196. case 0x1:
  2197. mpeg_decode_sequence_extension(s1);
  2198. break;
  2199. case 0x2:
  2200. mpeg_decode_sequence_display_extension(s1);
  2201. break;
  2202. case 0x3:
  2203. mpeg_decode_quant_matrix_extension(s);
  2204. break;
  2205. case 0x7:
  2206. mpeg_decode_picture_display_extension(s1);
  2207. break;
  2208. case 0x8:
  2209. mpeg_decode_picture_coding_extension(s);
  2210. break;
  2211. }
  2212. }
  2213. static void exchange_uv(MpegEncContext *s){
  2214. short * tmp = s->pblocks[4];
  2215. s->pblocks[4] = s->pblocks[5];
  2216. s->pblocks[5] = tmp;
  2217. }
  2218. static int mpeg_field_start(MpegEncContext *s){
  2219. AVCodecContext *avctx= s->avctx;
  2220. Mpeg1Context *s1 = (Mpeg1Context*)s;
  2221. /* start frame decoding */
  2222. if(s->first_field || s->picture_structure==PICT_FRAME){
  2223. if(MPV_frame_start(s, avctx) < 0)
  2224. return -1;
  2225. ff_er_frame_start(s);
  2226. /* first check if we must repeat the frame */
  2227. s->current_picture_ptr->repeat_pict = 0;
  2228. if (s->repeat_first_field) {
  2229. if (s->progressive_sequence) {
  2230. if (s->top_field_first)
  2231. s->current_picture_ptr->repeat_pict = 4;
  2232. else
  2233. s->current_picture_ptr->repeat_pict = 2;
  2234. } else if (s->progressive_frame) {
  2235. s->current_picture_ptr->repeat_pict = 1;
  2236. }
  2237. }
  2238. *s->current_picture_ptr->pan_scan= s1->pan_scan;
  2239. }else{ //second field
  2240. int i;
  2241. if(!s->current_picture_ptr){
  2242. av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
  2243. return -1;
  2244. }
  2245. for(i=0; i<4; i++){
  2246. s->current_picture.data[i] = s->current_picture_ptr->data[i];
  2247. if(s->picture_structure == PICT_BOTTOM_FIELD){
  2248. s->current_picture.data[i] += s->current_picture_ptr->linesize[i];
  2249. }
  2250. }
  2251. }
  2252. #ifdef HAVE_XVMC
  2253. // MPV_frame_start will call this function too,
  2254. // but we need to call it on every field
  2255. if(s->avctx->xvmc_acceleration)
  2256. XVMC_field_start(s,avctx);
  2257. #endif
  2258. return 0;
  2259. }
  2260. #define DECODE_SLICE_ERROR -1
  2261. #define DECODE_SLICE_OK 0
  2262. /**
  2263. * decodes a slice. MpegEncContext.mb_y must be set to the MB row from the startcode
  2264. * @return DECODE_SLICE_ERROR if the slice is damaged<br>
  2265. * DECODE_SLICE_OK if this slice is ok<br>
  2266. */
  2267. static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y,
  2268. const uint8_t **buf, int buf_size)
  2269. {
  2270. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2271. AVCodecContext *avctx= s->avctx;
  2272. int ret;
  2273. const int field_pic= s->picture_structure != PICT_FRAME;
  2274. const int lowres= s->avctx->lowres;
  2275. s->resync_mb_x=
  2276. s->resync_mb_y= -1;
  2277. if (mb_y<<field_pic >= s->mb_height){
  2278. av_log(s->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s->mb_height);
  2279. return -1;
  2280. }
  2281. init_get_bits(&s->gb, *buf, buf_size*8);
  2282. ff_mpeg1_clean_buffers(s);
  2283. s->interlaced_dct = 0;
  2284. s->qscale = get_qscale(s);
  2285. if(s->qscale == 0){
  2286. av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
  2287. return -1;
  2288. }
  2289. /* extra slice info */
  2290. while (get_bits1(&s->gb) != 0) {
  2291. skip_bits(&s->gb, 8);
  2292. }
  2293. s->mb_x=0;
  2294. for(;;) {
  2295. int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  2296. if (code < 0){
  2297. av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
  2298. return -1;
  2299. }
  2300. if (code >= 33) {
  2301. if (code == 33) {
  2302. s->mb_x += 33;
  2303. }
  2304. /* otherwise, stuffing, nothing to do */
  2305. } else {
  2306. s->mb_x += code;
  2307. break;
  2308. }
  2309. }
  2310. if(s->mb_x >= (unsigned)s->mb_width){
  2311. av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
  2312. return -1;
  2313. }
  2314. s->resync_mb_x= s->mb_x;
  2315. s->resync_mb_y= s->mb_y= mb_y;
  2316. s->mb_skip_run= 0;
  2317. ff_init_block_index(s);
  2318. if (s->mb_y==0 && s->mb_x==0 && (s->first_field || s->picture_structure==PICT_FRAME)) {
  2319. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  2320. av_log(s->avctx, AV_LOG_DEBUG, "qp:%d fc:%2d%2d%2d%2d %s %s %s %s %s dc:%d pstruct:%d fdct:%d cmv:%d qtype:%d ivlc:%d rff:%d %s\n",
  2321. s->qscale, s->mpeg_f_code[0][0],s->mpeg_f_code[0][1],s->mpeg_f_code[1][0],s->mpeg_f_code[1][1],
  2322. s->pict_type == I_TYPE ? "I" : (s->pict_type == P_TYPE ? "P" : (s->pict_type == B_TYPE ? "B" : "S")),
  2323. s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
  2324. s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
  2325. s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
  2326. }
  2327. }
  2328. for(;;) {
  2329. #ifdef HAVE_XVMC
  2330. //one 1 we memcpy blocks in xvmcvideo
  2331. if(s->avctx->xvmc_acceleration > 1)
  2332. XVMC_init_block(s);//set s->block
  2333. #endif
  2334. ret = mpeg_decode_mb(s, s->block);
  2335. s->chroma_qscale= s->qscale;
  2336. dprintf(s->avctx, "ret=%d\n", ret);
  2337. if (ret < 0)
  2338. return -1;
  2339. if(s->current_picture.motion_val[0] && !s->encoding){ //note motion_val is normally NULL unless we want to extract the MVs
  2340. const int wrap = field_pic ? 2*s->b8_stride : s->b8_stride;
  2341. int xy = s->mb_x*2 + s->mb_y*2*wrap;
  2342. int motion_x, motion_y, dir, i;
  2343. if(field_pic && !s->first_field)
  2344. xy += wrap/2;
  2345. for(i=0; i<2; i++){
  2346. for(dir=0; dir<2; dir++){
  2347. if (s->mb_intra || (dir==1 && s->pict_type != B_TYPE)) {
  2348. motion_x = motion_y = 0;
  2349. }else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)){
  2350. motion_x = s->mv[dir][0][0];
  2351. motion_y = s->mv[dir][0][1];
  2352. } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
  2353. motion_x = s->mv[dir][i][0];
  2354. motion_y = s->mv[dir][i][1];
  2355. }
  2356. s->current_picture.motion_val[dir][xy ][0] = motion_x;
  2357. s->current_picture.motion_val[dir][xy ][1] = motion_y;
  2358. s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
  2359. s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
  2360. s->current_picture.ref_index [dir][xy ]=
  2361. s->current_picture.ref_index [dir][xy + 1]= s->field_select[dir][i];
  2362. assert(s->field_select[dir][i]==0 || s->field_select[dir][i]==1);
  2363. }
  2364. xy += wrap;
  2365. }
  2366. }
  2367. s->dest[0] += 16 >> lowres;
  2368. s->dest[1] += 16 >> (s->chroma_x_shift + lowres);
  2369. s->dest[2] += 16 >> (s->chroma_x_shift + lowres);
  2370. MPV_decode_mb(s, s->block);
  2371. if (++s->mb_x >= s->mb_width) {
  2372. const int mb_size= 16>>s->avctx->lowres;
  2373. ff_draw_horiz_band(s, mb_size*s->mb_y, mb_size);
  2374. s->mb_x = 0;
  2375. s->mb_y++;
  2376. if(s->mb_y<<field_pic >= s->mb_height){
  2377. int left= s->gb.size_in_bits - get_bits_count(&s->gb);
  2378. int is_d10= s->chroma_format==2 && s->pict_type==I_TYPE && avctx->profile==0 && avctx->level==5
  2379. && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
  2380. && s->progressive_frame == 0 /* vbv_delay == 0xBBB || 0xE10*/;
  2381. if(left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
  2382. || (avctx->error_resilience >= FF_ER_AGGRESSIVE && left>8)){
  2383. av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
  2384. return -1;
  2385. }else
  2386. goto eos;
  2387. }
  2388. ff_init_block_index(s);
  2389. }
  2390. /* skip mb handling */
  2391. if (s->mb_skip_run == -1) {
  2392. /* read again increment */
  2393. s->mb_skip_run = 0;
  2394. for(;;) {
  2395. int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  2396. if (code < 0){
  2397. av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
  2398. return -1;
  2399. }
  2400. if (code >= 33) {
  2401. if (code == 33) {
  2402. s->mb_skip_run += 33;
  2403. }else if(code == 35){
  2404. if(s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0){
  2405. av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
  2406. return -1;
  2407. }
  2408. goto eos; /* end of slice */
  2409. }
  2410. /* otherwise, stuffing, nothing to do */
  2411. } else {
  2412. s->mb_skip_run += code;
  2413. break;
  2414. }
  2415. }
  2416. }
  2417. }
  2418. eos: // end of slice
  2419. *buf += get_bits_count(&s->gb)/8 - 1;
  2420. //printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
  2421. return 0;
  2422. }
  2423. static int slice_decode_thread(AVCodecContext *c, void *arg){
  2424. MpegEncContext *s= arg;
  2425. const uint8_t *buf= s->gb.buffer;
  2426. int mb_y= s->start_mb_y;
  2427. s->error_count= 3*(s->end_mb_y - s->start_mb_y)*s->mb_width;
  2428. for(;;){
  2429. uint32_t start_code;
  2430. int ret;
  2431. ret= mpeg_decode_slice((Mpeg1Context*)s, mb_y, &buf, s->gb.buffer_end - buf);
  2432. emms_c();
  2433. //av_log(c, AV_LOG_DEBUG, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
  2434. //ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, s->start_mb_y, s->end_mb_y, s->error_count);
  2435. if(ret < 0){
  2436. if(s->resync_mb_x>=0 && s->resync_mb_y>=0)
  2437. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, AC_ERROR|DC_ERROR|MV_ERROR);
  2438. }else{
  2439. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END|DC_END|MV_END);
  2440. }
  2441. if(s->mb_y == s->end_mb_y)
  2442. return 0;
  2443. start_code= -1;
  2444. buf = ff_find_start_code(buf, s->gb.buffer_end, &start_code);
  2445. mb_y= start_code - SLICE_MIN_START_CODE;
  2446. if(mb_y < 0 || mb_y >= s->end_mb_y)
  2447. return -1;
  2448. }
  2449. return 0; //not reached
  2450. }
  2451. /**
  2452. * handles slice ends.
  2453. * @return 1 if it seems to be the last slice of
  2454. */
  2455. static int slice_end(AVCodecContext *avctx, AVFrame *pict)
  2456. {
  2457. Mpeg1Context *s1 = avctx->priv_data;
  2458. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2459. if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
  2460. return 0;
  2461. #ifdef HAVE_XVMC
  2462. if(s->avctx->xvmc_acceleration)
  2463. XVMC_field_end(s);
  2464. #endif
  2465. /* end of slice reached */
  2466. if (/*s->mb_y<<field_pic == s->mb_height &&*/ !s->first_field) {
  2467. /* end of image */
  2468. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG2;
  2469. ff_er_frame_end(s);
  2470. MPV_frame_end(s);
  2471. if (s->pict_type == B_TYPE || s->low_delay) {
  2472. *pict= *(AVFrame*)s->current_picture_ptr;
  2473. ff_print_debug_info(s, pict);
  2474. } else {
  2475. s->picture_number++;
  2476. /* latency of 1 frame for I and P frames */
  2477. /* XXX: use another variable than picture_number */
  2478. if (s->last_picture_ptr != NULL) {
  2479. *pict= *(AVFrame*)s->last_picture_ptr;
  2480. ff_print_debug_info(s, pict);
  2481. }
  2482. }
  2483. return 1;
  2484. } else {
  2485. return 0;
  2486. }
  2487. }
  2488. static int mpeg1_decode_sequence(AVCodecContext *avctx,
  2489. const uint8_t *buf, int buf_size)
  2490. {
  2491. Mpeg1Context *s1 = avctx->priv_data;
  2492. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2493. int width,height;
  2494. int i, v, j;
  2495. init_get_bits(&s->gb, buf, buf_size*8);
  2496. width = get_bits(&s->gb, 12);
  2497. height = get_bits(&s->gb, 12);
  2498. if (width <= 0 || height <= 0 ||
  2499. (width % 2) != 0 || (height % 2) != 0)
  2500. return -1;
  2501. s->aspect_ratio_info= get_bits(&s->gb, 4);
  2502. if (s->aspect_ratio_info == 0)
  2503. return -1;
  2504. s->frame_rate_index = get_bits(&s->gb, 4);
  2505. if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
  2506. return -1;
  2507. s->bit_rate = get_bits(&s->gb, 18) * 400;
  2508. if (get_bits1(&s->gb) == 0) /* marker */
  2509. return -1;
  2510. s->width = width;
  2511. s->height = height;
  2512. s->avctx->rc_buffer_size= get_bits(&s->gb, 10) * 1024*16;
  2513. skip_bits(&s->gb, 1);
  2514. /* get matrix */
  2515. if (get_bits1(&s->gb)) {
  2516. for(i=0;i<64;i++) {
  2517. v = get_bits(&s->gb, 8);
  2518. if(v==0){
  2519. av_log(s->avctx, AV_LOG_ERROR, "intra matrix damaged\n");
  2520. return -1;
  2521. }
  2522. j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  2523. s->intra_matrix[j] = v;
  2524. s->chroma_intra_matrix[j] = v;
  2525. }
  2526. #ifdef DEBUG
  2527. dprintf(s->avctx, "intra matrix present\n");
  2528. for(i=0;i<64;i++)
  2529. dprintf(s->avctx, " %d", s->intra_matrix[s->dsp.idct_permutation[i]]);
  2530. dprintf(s->avctx, "\n");
  2531. #endif
  2532. } else {
  2533. for(i=0;i<64;i++) {
  2534. j = s->dsp.idct_permutation[i];
  2535. v = ff_mpeg1_default_intra_matrix[i];
  2536. s->intra_matrix[j] = v;
  2537. s->chroma_intra_matrix[j] = v;
  2538. }
  2539. }
  2540. if (get_bits1(&s->gb)) {
  2541. for(i=0;i<64;i++) {
  2542. v = get_bits(&s->gb, 8);
  2543. if(v==0){
  2544. av_log(s->avctx, AV_LOG_ERROR, "inter matrix damaged\n");
  2545. return -1;
  2546. }
  2547. j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  2548. s->inter_matrix[j] = v;
  2549. s->chroma_inter_matrix[j] = v;
  2550. }
  2551. #ifdef DEBUG
  2552. dprintf(s->avctx, "non intra matrix present\n");
  2553. for(i=0;i<64;i++)
  2554. dprintf(s->avctx, " %d", s->inter_matrix[s->dsp.idct_permutation[i]]);
  2555. dprintf(s->avctx, "\n");
  2556. #endif
  2557. } else {
  2558. for(i=0;i<64;i++) {
  2559. int j= s->dsp.idct_permutation[i];
  2560. v = ff_mpeg1_default_non_intra_matrix[i];
  2561. s->inter_matrix[j] = v;
  2562. s->chroma_inter_matrix[j] = v;
  2563. }
  2564. }
  2565. if(show_bits(&s->gb, 23) != 0){
  2566. av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
  2567. return -1;
  2568. }
  2569. /* we set mpeg2 parameters so that it emulates mpeg1 */
  2570. s->progressive_sequence = 1;
  2571. s->progressive_frame = 1;
  2572. s->picture_structure = PICT_FRAME;
  2573. s->frame_pred_frame_dct = 1;
  2574. s->chroma_format = 1;
  2575. s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG1VIDEO;
  2576. avctx->sub_id = 1; /* indicates mpeg1 */
  2577. s->out_format = FMT_MPEG1;
  2578. s->swap_uv = 0;//AFAIK VCR2 don't have SEQ_HEADER
  2579. if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
  2580. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  2581. av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
  2582. s->avctx->rc_buffer_size, s->bit_rate);
  2583. return 0;
  2584. }
  2585. static int vcr2_init_sequence(AVCodecContext *avctx)
  2586. {
  2587. Mpeg1Context *s1 = avctx->priv_data;
  2588. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2589. int i, v;
  2590. /* start new mpeg1 context decoding */
  2591. s->out_format = FMT_MPEG1;
  2592. if (s1->mpeg_enc_ctx_allocated) {
  2593. MPV_common_end(s);
  2594. }
  2595. s->width = avctx->coded_width;
  2596. s->height = avctx->coded_height;
  2597. avctx->has_b_frames= 0; //true?
  2598. s->low_delay= 1;
  2599. if(avctx->xvmc_acceleration){
  2600. avctx->pix_fmt = avctx->get_format(avctx,pixfmt_xvmc_mpg2_420);
  2601. }else{
  2602. avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_420);
  2603. }
  2604. if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT )
  2605. if( avctx->idct_algo == FF_IDCT_AUTO )
  2606. avctx->idct_algo = FF_IDCT_SIMPLE;
  2607. if (MPV_common_init(s) < 0)
  2608. return -1;
  2609. exchange_uv(s);//common init reset pblocks, so we swap them here
  2610. s->swap_uv = 1;// in case of xvmc we need to swap uv for each MB
  2611. s1->mpeg_enc_ctx_allocated = 1;
  2612. for(i=0;i<64;i++) {
  2613. int j= s->dsp.idct_permutation[i];
  2614. v = ff_mpeg1_default_intra_matrix[i];
  2615. s->intra_matrix[j] = v;
  2616. s->chroma_intra_matrix[j] = v;
  2617. v = ff_mpeg1_default_non_intra_matrix[i];
  2618. s->inter_matrix[j] = v;
  2619. s->chroma_inter_matrix[j] = v;
  2620. }
  2621. s->progressive_sequence = 1;
  2622. s->progressive_frame = 1;
  2623. s->picture_structure = PICT_FRAME;
  2624. s->frame_pred_frame_dct = 1;
  2625. s->chroma_format = 1;
  2626. s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
  2627. avctx->sub_id = 2; /* indicates mpeg2 */
  2628. return 0;
  2629. }
  2630. static void mpeg_decode_user_data(AVCodecContext *avctx,
  2631. const uint8_t *buf, int buf_size)
  2632. {
  2633. const uint8_t *p;
  2634. int len, flags;
  2635. p = buf;
  2636. len = buf_size;
  2637. /* we parse the DTG active format information */
  2638. if (len >= 5 &&
  2639. p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
  2640. flags = p[4];
  2641. p += 5;
  2642. len -= 5;
  2643. if (flags & 0x80) {
  2644. /* skip event id */
  2645. if (len < 2)
  2646. return;
  2647. p += 2;
  2648. len -= 2;
  2649. }
  2650. if (flags & 0x40) {
  2651. if (len < 1)
  2652. return;
  2653. avctx->dtg_active_format = p[0] & 0x0f;
  2654. }
  2655. }
  2656. }
  2657. static void mpeg_decode_gop(AVCodecContext *avctx,
  2658. const uint8_t *buf, int buf_size){
  2659. Mpeg1Context *s1 = avctx->priv_data;
  2660. MpegEncContext *s = &s1->mpeg_enc_ctx;
  2661. int drop_frame_flag;
  2662. int time_code_hours, time_code_minutes;
  2663. int time_code_seconds, time_code_pictures;
  2664. int broken_link;
  2665. init_get_bits(&s->gb, buf, buf_size*8);
  2666. drop_frame_flag = get_bits1(&s->gb);
  2667. time_code_hours=get_bits(&s->gb,5);
  2668. time_code_minutes = get_bits(&s->gb,6);
  2669. skip_bits1(&s->gb);//marker bit
  2670. time_code_seconds = get_bits(&s->gb,6);
  2671. time_code_pictures = get_bits(&s->gb,6);
  2672. /*broken_link indicate that after editing the
  2673. reference frames of the first B-Frames after GOP I-Frame
  2674. are missing (open gop)*/
  2675. broken_link = get_bits1(&s->gb);
  2676. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  2677. av_log(s->avctx, AV_LOG_DEBUG, "GOP (%2d:%02d:%02d.[%02d]) broken_link=%d\n",
  2678. time_code_hours, time_code_minutes, time_code_seconds,
  2679. time_code_pictures, broken_link);
  2680. }
  2681. /**
  2682. * finds the end of the current frame in the bitstream.
  2683. * @return the position of the first byte of the next frame, or -1
  2684. */
  2685. int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
  2686. {
  2687. int i;
  2688. uint32_t state= pc->state;
  2689. i=0;
  2690. if(!pc->frame_start_found){
  2691. for(i=0; i<buf_size; i++){
  2692. i= ff_find_start_code(buf+i, buf+buf_size, &state) - buf - 1;
  2693. if(state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE){
  2694. i++;
  2695. pc->frame_start_found=1;
  2696. break;
  2697. }
  2698. }
  2699. }
  2700. if(pc->frame_start_found){
  2701. /* EOF considered as end of frame */
  2702. if (buf_size == 0)
  2703. return 0;
  2704. for(; i<buf_size; i++){
  2705. i= ff_find_start_code(buf+i, buf+buf_size, &state) - buf - 1;
  2706. if((state&0xFFFFFF00) == 0x100){
  2707. if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
  2708. pc->frame_start_found=0;
  2709. pc->state=-1;
  2710. return i-3;
  2711. }
  2712. }
  2713. }
  2714. }
  2715. pc->state= state;
  2716. return END_NOT_FOUND;
  2717. }
  2718. /* handle buffering and image synchronisation */
  2719. static int mpeg_decode_frame(AVCodecContext *avctx,
  2720. void *data, int *data_size,
  2721. uint8_t *buf, int buf_size)
  2722. {
  2723. Mpeg1Context *s = avctx->priv_data;
  2724. const uint8_t *buf_end;
  2725. const uint8_t *buf_ptr;
  2726. uint32_t start_code;
  2727. int ret, input_size;
  2728. AVFrame *picture = data;
  2729. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  2730. dprintf(avctx, "fill_buffer\n");
  2731. if (buf_size == 0) {
  2732. /* special case for last picture */
  2733. if (s2->low_delay==0 && s2->next_picture_ptr) {
  2734. *picture= *(AVFrame*)s2->next_picture_ptr;
  2735. s2->next_picture_ptr= NULL;
  2736. *data_size = sizeof(AVFrame);
  2737. }
  2738. return 0;
  2739. }
  2740. if(s2->flags&CODEC_FLAG_TRUNCATED){
  2741. int next= ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size);
  2742. if( ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0 )
  2743. return buf_size;
  2744. }
  2745. buf_ptr = buf;
  2746. buf_end = buf + buf_size;
  2747. #if 0
  2748. if (s->repeat_field % 2 == 1) {
  2749. s->repeat_field++;
  2750. //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
  2751. // s2->picture_number, s->repeat_field);
  2752. if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
  2753. *data_size = sizeof(AVPicture);
  2754. goto the_end;
  2755. }
  2756. }
  2757. #endif
  2758. if(s->mpeg_enc_ctx_allocated==0 && avctx->codec_tag == ff_get_fourcc("VCR2"))
  2759. vcr2_init_sequence(avctx);
  2760. s->slice_count= 0;
  2761. for(;;) {
  2762. /* find start next code */
  2763. start_code = -1;
  2764. buf_ptr = ff_find_start_code(buf_ptr,buf_end, &start_code);
  2765. if (start_code > 0x1ff){
  2766. if(s2->pict_type != B_TYPE || avctx->skip_frame <= AVDISCARD_DEFAULT){
  2767. if(avctx->thread_count > 1){
  2768. int i;
  2769. avctx->execute(avctx, slice_decode_thread, (void**)&(s2->thread_context[0]), NULL, s->slice_count);
  2770. for(i=0; i<s->slice_count; i++)
  2771. s2->error_count += s2->thread_context[i]->error_count;
  2772. }
  2773. if (slice_end(avctx, picture)) {
  2774. if(s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
  2775. *data_size = sizeof(AVPicture);
  2776. }
  2777. }
  2778. return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
  2779. }
  2780. input_size = buf_end - buf_ptr;
  2781. if(avctx->debug & FF_DEBUG_STARTCODE){
  2782. av_log(avctx, AV_LOG_DEBUG, "%3X at %zd left %d\n", start_code, buf_ptr-buf, input_size);
  2783. }
  2784. /* prepare data for next start code */
  2785. switch(start_code) {
  2786. case SEQ_START_CODE:
  2787. mpeg1_decode_sequence(avctx, buf_ptr,
  2788. input_size);
  2789. break;
  2790. case PICTURE_START_CODE:
  2791. /* we have a complete image : we try to decompress it */
  2792. mpeg1_decode_picture(avctx,
  2793. buf_ptr, input_size);
  2794. break;
  2795. case EXT_START_CODE:
  2796. mpeg_decode_extension(avctx,
  2797. buf_ptr, input_size);
  2798. break;
  2799. case USER_START_CODE:
  2800. mpeg_decode_user_data(avctx,
  2801. buf_ptr, input_size);
  2802. break;
  2803. case GOP_START_CODE:
  2804. s2->first_field=0;
  2805. mpeg_decode_gop(avctx,
  2806. buf_ptr, input_size);
  2807. break;
  2808. default:
  2809. if (start_code >= SLICE_MIN_START_CODE &&
  2810. start_code <= SLICE_MAX_START_CODE) {
  2811. int mb_y= start_code - SLICE_MIN_START_CODE;
  2812. if(s2->last_picture_ptr==NULL){
  2813. /* Skip B-frames if we do not have reference frames. */
  2814. if(s2->pict_type==B_TYPE) break;
  2815. /* Skip P-frames if we do not have reference frame no valid header. */
  2816. // if(s2->pict_type==P_TYPE && s2->first_field && !s2->first_slice) break;
  2817. }
  2818. /* Skip B-frames if we are in a hurry. */
  2819. if(avctx->hurry_up && s2->pict_type==B_TYPE) break;
  2820. if( (avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type==B_TYPE)
  2821. ||(avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type!=I_TYPE)
  2822. || avctx->skip_frame >= AVDISCARD_ALL)
  2823. break;
  2824. /* Skip everything if we are in a hurry>=5. */
  2825. if(avctx->hurry_up>=5) break;
  2826. if (!s->mpeg_enc_ctx_allocated) break;
  2827. if(s2->codec_id == CODEC_ID_MPEG2VIDEO){
  2828. if(mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
  2829. break;
  2830. }
  2831. if(s2->first_slice){
  2832. s2->first_slice=0;
  2833. if(mpeg_field_start(s2) < 0)
  2834. return -1;
  2835. }
  2836. if(!s2->current_picture_ptr){
  2837. av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\n");
  2838. return -1;
  2839. }
  2840. if(avctx->thread_count > 1){
  2841. int threshold= (s2->mb_height*s->slice_count + avctx->thread_count/2) / avctx->thread_count;
  2842. if(threshold <= mb_y){
  2843. MpegEncContext *thread_context= s2->thread_context[s->slice_count];
  2844. thread_context->start_mb_y= mb_y;
  2845. thread_context->end_mb_y = s2->mb_height;
  2846. if(s->slice_count){
  2847. s2->thread_context[s->slice_count-1]->end_mb_y= mb_y;
  2848. ff_update_duplicate_context(thread_context, s2);
  2849. }
  2850. init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
  2851. s->slice_count++;
  2852. }
  2853. buf_ptr += 2; //FIXME add minimum num of bytes per slice
  2854. }else{
  2855. ret = mpeg_decode_slice(s, mb_y, &buf_ptr, input_size);
  2856. emms_c();
  2857. if(ret < 0){
  2858. if(s2->resync_mb_x>=0 && s2->resync_mb_y>=0)
  2859. ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x, s2->mb_y, AC_ERROR|DC_ERROR|MV_ERROR);
  2860. }else{
  2861. ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x-1, s2->mb_y, AC_END|DC_END|MV_END);
  2862. }
  2863. }
  2864. }
  2865. break;
  2866. }
  2867. }
  2868. }
  2869. static int mpeg_decode_end(AVCodecContext *avctx)
  2870. {
  2871. Mpeg1Context *s = avctx->priv_data;
  2872. if (s->mpeg_enc_ctx_allocated)
  2873. MPV_common_end(&s->mpeg_enc_ctx);
  2874. return 0;
  2875. }
  2876. AVCodec mpeg1video_decoder = {
  2877. "mpeg1video",
  2878. CODEC_TYPE_VIDEO,
  2879. CODEC_ID_MPEG1VIDEO,
  2880. sizeof(Mpeg1Context),
  2881. mpeg_decode_init,
  2882. NULL,
  2883. mpeg_decode_end,
  2884. mpeg_decode_frame,
  2885. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  2886. .flush= ff_mpeg_flush,
  2887. };
  2888. AVCodec mpeg2video_decoder = {
  2889. "mpeg2video",
  2890. CODEC_TYPE_VIDEO,
  2891. CODEC_ID_MPEG2VIDEO,
  2892. sizeof(Mpeg1Context),
  2893. mpeg_decode_init,
  2894. NULL,
  2895. mpeg_decode_end,
  2896. mpeg_decode_frame,
  2897. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  2898. .flush= ff_mpeg_flush,
  2899. };
  2900. //legacy decoder
  2901. AVCodec mpegvideo_decoder = {
  2902. "mpegvideo",
  2903. CODEC_TYPE_VIDEO,
  2904. CODEC_ID_MPEG2VIDEO,
  2905. sizeof(Mpeg1Context),
  2906. mpeg_decode_init,
  2907. NULL,
  2908. mpeg_decode_end,
  2909. mpeg_decode_frame,
  2910. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  2911. .flush= ff_mpeg_flush,
  2912. };
  2913. #ifdef CONFIG_ENCODERS
  2914. AVCodec mpeg1video_encoder = {
  2915. "mpeg1video",
  2916. CODEC_TYPE_VIDEO,
  2917. CODEC_ID_MPEG1VIDEO,
  2918. sizeof(MpegEncContext),
  2919. encode_init,
  2920. MPV_encode_picture,
  2921. MPV_encode_end,
  2922. .supported_framerates= ff_frame_rate_tab+1,
  2923. .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
  2924. .capabilities= CODEC_CAP_DELAY,
  2925. };
  2926. AVCodec mpeg2video_encoder = {
  2927. "mpeg2video",
  2928. CODEC_TYPE_VIDEO,
  2929. CODEC_ID_MPEG2VIDEO,
  2930. sizeof(MpegEncContext),
  2931. encode_init,
  2932. MPV_encode_picture,
  2933. MPV_encode_end,
  2934. .supported_framerates= ff_frame_rate_tab+1,
  2935. .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, -1},
  2936. .capabilities= CODEC_CAP_DELAY,
  2937. };
  2938. #endif
  2939. #ifdef HAVE_XVMC
  2940. static int mpeg_mc_decode_init(AVCodecContext *avctx){
  2941. Mpeg1Context *s;
  2942. if( avctx->thread_count > 1)
  2943. return -1;
  2944. if( !(avctx->slice_flags & SLICE_FLAG_CODED_ORDER) )
  2945. return -1;
  2946. if( !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD) ){
  2947. dprintf(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
  2948. }
  2949. mpeg_decode_init(avctx);
  2950. s = avctx->priv_data;
  2951. avctx->pix_fmt = PIX_FMT_XVMC_MPEG2_IDCT;
  2952. avctx->xvmc_acceleration = 2;//2 - the blocks are packed!
  2953. return 0;
  2954. }
  2955. AVCodec mpeg_xvmc_decoder = {
  2956. "mpegvideo_xvmc",
  2957. CODEC_TYPE_VIDEO,
  2958. CODEC_ID_MPEG2VIDEO_XVMC,
  2959. sizeof(Mpeg1Context),
  2960. mpeg_mc_decode_init,
  2961. NULL,
  2962. mpeg_decode_end,
  2963. mpeg_decode_frame,
  2964. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
  2965. .flush= ff_mpeg_flush,
  2966. };
  2967. #endif
  2968. /* this is ugly i know, but the alternative is too make
  2969. hundreds of vars global and prefix them with ff_mpeg1_
  2970. which is far uglier. */
  2971. #include "mdec.c"