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.

3317 lines
113KB

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