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.

3498 lines
119KB

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