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.

3519 lines
120KB

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