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.

2520 lines
84KB

  1. /*
  2. * MPEG1/2 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 decoder
  25. */
  26. //#define DEBUG
  27. #include "avcodec.h"
  28. #include "dsputil.h"
  29. #include "mpegvideo.h"
  30. #include "mpeg12.h"
  31. #include "mpeg12data.h"
  32. #include "mpeg12decdata.h"
  33. #include "bytestream.h"
  34. //#undef NDEBUG
  35. //#include <assert.h>
  36. #define DC_VLC_BITS 9
  37. #define MV_VLC_BITS 9
  38. #define MBINCR_VLC_BITS 9
  39. #define MB_PAT_VLC_BITS 9
  40. #define MB_PTYPE_VLC_BITS 6
  41. #define MB_BTYPE_VLC_BITS 6
  42. #define TEX_VLC_BITS 9
  43. static inline int mpeg1_decode_block_inter(MpegEncContext *s,
  44. DCTELEM *block,
  45. int n);
  46. static inline int mpeg1_decode_block_intra(MpegEncContext *s,
  47. DCTELEM *block,
  48. int n);
  49. static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n);
  50. static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
  51. DCTELEM *block,
  52. int n);
  53. static inline int mpeg2_decode_block_intra(MpegEncContext *s,
  54. DCTELEM *block,
  55. int n);
  56. static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s, DCTELEM *block, int n);
  57. static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n);
  58. static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred);
  59. static void exchange_uv(MpegEncContext *s);
  60. extern int XVMC_field_start(MpegEncContext *s, AVCodecContext *avctx);
  61. extern int XVMC_field_end(MpegEncContext *s);
  62. extern void XVMC_pack_pblocks(MpegEncContext *s,int cbp);
  63. extern void XVMC_init_block(MpegEncContext *s);//set s->block
  64. static const enum PixelFormat pixfmt_yuv_420[]= {PIX_FMT_YUV420P,PIX_FMT_NONE};
  65. static const enum PixelFormat pixfmt_yuv_422[]= {PIX_FMT_YUV422P,PIX_FMT_NONE};
  66. static const enum PixelFormat pixfmt_yuv_444[]= {PIX_FMT_YUV444P,PIX_FMT_NONE};
  67. static const enum PixelFormat pixfmt_xvmc_mpg2_420[] = {
  68. PIX_FMT_XVMC_MPEG2_IDCT,
  69. PIX_FMT_XVMC_MPEG2_MC,
  70. PIX_FMT_NONE};
  71. uint8_t ff_mpeg12_static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 3];
  72. #define INIT_2D_VLC_RL(rl, static_size)\
  73. {\
  74. static RL_VLC_ELEM rl_vlc_table[static_size];\
  75. INIT_VLC_STATIC(&rl.vlc, TEX_VLC_BITS, rl.n + 2,\
  76. &rl.table_vlc[0][1], 4, 2,\
  77. &rl.table_vlc[0][0], 4, 2, static_size);\
  78. \
  79. rl.rl_vlc[0]= rl_vlc_table;\
  80. init_2d_vlc_rl(&rl);\
  81. }
  82. static void init_2d_vlc_rl(RLTable *rl)
  83. {
  84. int i;
  85. for(i=0; i<rl->vlc.table_size; i++){
  86. int code= rl->vlc.table[i][0];
  87. int len = rl->vlc.table[i][1];
  88. int level, run;
  89. if(len==0){ // illegal code
  90. run= 65;
  91. level= MAX_LEVEL;
  92. }else if(len<0){ //more bits needed
  93. run= 0;
  94. level= code;
  95. }else{
  96. if(code==rl->n){ //esc
  97. run= 65;
  98. level= 0;
  99. }else if(code==rl->n+1){ //eob
  100. run= 0;
  101. level= 127;
  102. }else{
  103. run= rl->table_run [code] + 1;
  104. level= rl->table_level[code];
  105. }
  106. }
  107. rl->rl_vlc[0][i].len= len;
  108. rl->rl_vlc[0][i].level= level;
  109. rl->rl_vlc[0][i].run= run;
  110. }
  111. }
  112. void ff_mpeg12_common_init(MpegEncContext *s)
  113. {
  114. s->y_dc_scale_table=
  115. s->c_dc_scale_table= mpeg2_dc_scale_table[s->intra_dc_precision];
  116. }
  117. void ff_mpeg1_clean_buffers(MpegEncContext *s){
  118. s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
  119. s->last_dc[1] = s->last_dc[0];
  120. s->last_dc[2] = s->last_dc[0];
  121. memset(s->last_mv, 0, sizeof(s->last_mv));
  122. }
  123. /******************************************/
  124. /* decoding */
  125. static VLC dc_lum_vlc;
  126. static VLC dc_chroma_vlc;
  127. static VLC mv_vlc;
  128. static VLC mbincr_vlc;
  129. static VLC mb_ptype_vlc;
  130. static VLC mb_btype_vlc;
  131. static VLC mb_pat_vlc;
  132. static void init_vlcs(void)
  133. {
  134. static int done = 0;
  135. if (!done) {
  136. done = 1;
  137. INIT_VLC_STATIC(&dc_lum_vlc, DC_VLC_BITS, 12,
  138. ff_mpeg12_vlc_dc_lum_bits, 1, 1,
  139. ff_mpeg12_vlc_dc_lum_code, 2, 2, 512);
  140. INIT_VLC_STATIC(&dc_chroma_vlc, DC_VLC_BITS, 12,
  141. ff_mpeg12_vlc_dc_chroma_bits, 1, 1,
  142. ff_mpeg12_vlc_dc_chroma_code, 2, 2, 514);
  143. INIT_VLC_STATIC(&mv_vlc, MV_VLC_BITS, 17,
  144. &ff_mpeg12_mbMotionVectorTable[0][1], 2, 1,
  145. &ff_mpeg12_mbMotionVectorTable[0][0], 2, 1, 518);
  146. INIT_VLC_STATIC(&mbincr_vlc, MBINCR_VLC_BITS, 36,
  147. &ff_mpeg12_mbAddrIncrTable[0][1], 2, 1,
  148. &ff_mpeg12_mbAddrIncrTable[0][0], 2, 1, 538);
  149. INIT_VLC_STATIC(&mb_pat_vlc, MB_PAT_VLC_BITS, 64,
  150. &ff_mpeg12_mbPatTable[0][1], 2, 1,
  151. &ff_mpeg12_mbPatTable[0][0], 2, 1, 512);
  152. INIT_VLC_STATIC(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
  153. &table_mb_ptype[0][1], 2, 1,
  154. &table_mb_ptype[0][0], 2, 1, 64);
  155. INIT_VLC_STATIC(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,
  156. &table_mb_btype[0][1], 2, 1,
  157. &table_mb_btype[0][0], 2, 1, 64);
  158. init_rl(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
  159. init_rl(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]);
  160. INIT_2D_VLC_RL(ff_rl_mpeg1, 680);
  161. INIT_2D_VLC_RL(ff_rl_mpeg2, 674);
  162. }
  163. }
  164. static inline int get_dmv(MpegEncContext *s)
  165. {
  166. if(get_bits1(&s->gb))
  167. return 1 - (get_bits1(&s->gb) << 1);
  168. else
  169. return 0;
  170. }
  171. static inline int get_qscale(MpegEncContext *s)
  172. {
  173. int qscale = get_bits(&s->gb, 5);
  174. if (s->q_scale_type) {
  175. return non_linear_qscale[qscale];
  176. } else {
  177. return qscale << 1;
  178. }
  179. }
  180. /* motion type (for mpeg2) */
  181. #define MT_FIELD 1
  182. #define MT_FRAME 2
  183. #define MT_16X8 2
  184. #define MT_DMV 3
  185. static int mpeg_decode_mb(MpegEncContext *s,
  186. DCTELEM block[12][64])
  187. {
  188. int i, j, k, cbp, val, mb_type, motion_type;
  189. const int mb_block_count = 4 + (1<< s->chroma_format);
  190. dprintf(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
  191. assert(s->mb_skipped==0);
  192. if (s->mb_skip_run-- != 0) {
  193. if (s->pict_type == FF_P_TYPE) {
  194. s->mb_skipped = 1;
  195. s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
  196. } else {
  197. int mb_type;
  198. if(s->mb_x)
  199. mb_type= s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1];
  200. else
  201. 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,
  202. if(IS_INTRA(mb_type))
  203. return -1;
  204. s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]=
  205. mb_type | MB_TYPE_SKIP;
  206. // assert(s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1]&(MB_TYPE_16x16|MB_TYPE_16x8));
  207. if((s->mv[0][0][0]|s->mv[0][0][1]|s->mv[1][0][0]|s->mv[1][0][1])==0)
  208. s->mb_skipped = 1;
  209. }
  210. return 0;
  211. }
  212. switch(s->pict_type) {
  213. default:
  214. case FF_I_TYPE:
  215. if (get_bits1(&s->gb) == 0) {
  216. if (get_bits1(&s->gb) == 0){
  217. av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
  218. return -1;
  219. }
  220. mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
  221. } else {
  222. mb_type = MB_TYPE_INTRA;
  223. }
  224. break;
  225. case FF_P_TYPE:
  226. mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
  227. if (mb_type < 0){
  228. av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
  229. return -1;
  230. }
  231. mb_type = ptype2mb_type[ mb_type ];
  232. break;
  233. case FF_B_TYPE:
  234. mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
  235. if (mb_type < 0){
  236. av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
  237. return -1;
  238. }
  239. mb_type = btype2mb_type[ mb_type ];
  240. break;
  241. }
  242. dprintf(s->avctx, "mb_type=%x\n", mb_type);
  243. // motion_type = 0; /* avoid warning */
  244. if (IS_INTRA(mb_type)) {
  245. s->dsp.clear_blocks(s->block[0]);
  246. if(!s->chroma_y_shift){
  247. s->dsp.clear_blocks(s->block[6]);
  248. }
  249. /* compute dct type */
  250. if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
  251. !s->frame_pred_frame_dct) {
  252. s->interlaced_dct = get_bits1(&s->gb);
  253. }
  254. if (IS_QUANT(mb_type))
  255. s->qscale = get_qscale(s);
  256. if (s->concealment_motion_vectors) {
  257. /* just parse them */
  258. if (s->picture_structure != PICT_FRAME)
  259. skip_bits1(&s->gb); /* field select */
  260. s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
  261. mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
  262. s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
  263. mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
  264. skip_bits1(&s->gb); /* marker */
  265. }else
  266. memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
  267. s->mb_intra = 1;
  268. #ifdef HAVE_XVMC
  269. //one 1 we memcpy blocks in xvmcvideo
  270. if(s->avctx->xvmc_acceleration > 1){
  271. XVMC_pack_pblocks(s,-1);//inter are always full blocks
  272. if(s->swap_uv){
  273. exchange_uv(s);
  274. }
  275. }
  276. #endif
  277. if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
  278. if(s->flags2 & CODEC_FLAG2_FAST){
  279. for(i=0;i<6;i++) {
  280. mpeg2_fast_decode_block_intra(s, s->pblocks[i], i);
  281. }
  282. }else{
  283. for(i=0;i<mb_block_count;i++) {
  284. if (mpeg2_decode_block_intra(s, s->pblocks[i], i) < 0)
  285. return -1;
  286. }
  287. }
  288. } else {
  289. for(i=0;i<6;i++) {
  290. if (mpeg1_decode_block_intra(s, s->pblocks[i], i) < 0)
  291. return -1;
  292. }
  293. }
  294. } else {
  295. if (mb_type & MB_TYPE_ZERO_MV){
  296. assert(mb_type & MB_TYPE_CBP);
  297. s->mv_dir = MV_DIR_FORWARD;
  298. if(s->picture_structure == PICT_FRAME){
  299. if(!s->frame_pred_frame_dct)
  300. s->interlaced_dct = get_bits1(&s->gb);
  301. s->mv_type = MV_TYPE_16X16;
  302. }else{
  303. s->mv_type = MV_TYPE_FIELD;
  304. mb_type |= MB_TYPE_INTERLACED;
  305. s->field_select[0][0]= s->picture_structure - 1;
  306. }
  307. if (IS_QUANT(mb_type))
  308. s->qscale = get_qscale(s);
  309. s->last_mv[0][0][0] = 0;
  310. s->last_mv[0][0][1] = 0;
  311. s->last_mv[0][1][0] = 0;
  312. s->last_mv[0][1][1] = 0;
  313. s->mv[0][0][0] = 0;
  314. s->mv[0][0][1] = 0;
  315. }else{
  316. assert(mb_type & MB_TYPE_L0L1);
  317. //FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
  318. /* get additional motion vector type */
  319. if (s->frame_pred_frame_dct)
  320. motion_type = MT_FRAME;
  321. else{
  322. motion_type = get_bits(&s->gb, 2);
  323. if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
  324. s->interlaced_dct = get_bits1(&s->gb);
  325. }
  326. if (IS_QUANT(mb_type))
  327. s->qscale = get_qscale(s);
  328. /* motion vectors */
  329. s->mv_dir= (mb_type>>13)&3;
  330. dprintf(s->avctx, "motion_type=%d\n", motion_type);
  331. switch(motion_type) {
  332. case MT_FRAME: /* or MT_16X8 */
  333. if (s->picture_structure == PICT_FRAME) {
  334. mb_type |= MB_TYPE_16x16;
  335. s->mv_type = MV_TYPE_16X16;
  336. for(i=0;i<2;i++) {
  337. if (USES_LIST(mb_type, i)) {
  338. /* MT_FRAME */
  339. s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] =
  340. mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
  341. s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] =
  342. mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
  343. /* full_pel: only for mpeg1 */
  344. if (s->full_pel[i]){
  345. s->mv[i][0][0] <<= 1;
  346. s->mv[i][0][1] <<= 1;
  347. }
  348. }
  349. }
  350. } else {
  351. mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
  352. s->mv_type = MV_TYPE_16X8;
  353. for(i=0;i<2;i++) {
  354. if (USES_LIST(mb_type, i)) {
  355. /* MT_16X8 */
  356. for(j=0;j<2;j++) {
  357. s->field_select[i][j] = get_bits1(&s->gb);
  358. for(k=0;k<2;k++) {
  359. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  360. s->last_mv[i][j][k]);
  361. s->last_mv[i][j][k] = val;
  362. s->mv[i][j][k] = val;
  363. }
  364. }
  365. }
  366. }
  367. }
  368. break;
  369. case MT_FIELD:
  370. s->mv_type = MV_TYPE_FIELD;
  371. if (s->picture_structure == PICT_FRAME) {
  372. mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
  373. for(i=0;i<2;i++) {
  374. if (USES_LIST(mb_type, i)) {
  375. for(j=0;j<2;j++) {
  376. s->field_select[i][j] = get_bits1(&s->gb);
  377. val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  378. s->last_mv[i][j][0]);
  379. s->last_mv[i][j][0] = val;
  380. s->mv[i][j][0] = val;
  381. dprintf(s->avctx, "fmx=%d\n", val);
  382. val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  383. s->last_mv[i][j][1] >> 1);
  384. s->last_mv[i][j][1] = val << 1;
  385. s->mv[i][j][1] = val;
  386. dprintf(s->avctx, "fmy=%d\n", val);
  387. }
  388. }
  389. }
  390. } else {
  391. mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
  392. for(i=0;i<2;i++) {
  393. if (USES_LIST(mb_type, i)) {
  394. s->field_select[i][0] = get_bits1(&s->gb);
  395. for(k=0;k<2;k++) {
  396. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  397. s->last_mv[i][0][k]);
  398. s->last_mv[i][0][k] = val;
  399. s->last_mv[i][1][k] = val;
  400. s->mv[i][0][k] = val;
  401. }
  402. }
  403. }
  404. }
  405. break;
  406. case MT_DMV:
  407. s->mv_type = MV_TYPE_DMV;
  408. for(i=0;i<2;i++) {
  409. if (USES_LIST(mb_type, i)) {
  410. int dmx, dmy, mx, my, m;
  411. mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  412. s->last_mv[i][0][0]);
  413. s->last_mv[i][0][0] = mx;
  414. s->last_mv[i][1][0] = mx;
  415. dmx = get_dmv(s);
  416. my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  417. s->last_mv[i][0][1] >> 1);
  418. dmy = get_dmv(s);
  419. s->last_mv[i][0][1] = my<<1;
  420. s->last_mv[i][1][1] = my<<1;
  421. s->mv[i][0][0] = mx;
  422. s->mv[i][0][1] = my;
  423. s->mv[i][1][0] = mx;//not used
  424. s->mv[i][1][1] = my;//not used
  425. if (s->picture_structure == PICT_FRAME) {
  426. mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
  427. //m = 1 + 2 * s->top_field_first;
  428. m = s->top_field_first ? 1 : 3;
  429. /* top -> top pred */
  430. s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  431. s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
  432. m = 4 - m;
  433. s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  434. s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
  435. } else {
  436. mb_type |= MB_TYPE_16x16;
  437. s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
  438. s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
  439. if(s->picture_structure == PICT_TOP_FIELD)
  440. s->mv[i][2][1]--;
  441. else
  442. s->mv[i][2][1]++;
  443. }
  444. }
  445. }
  446. break;
  447. default:
  448. av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
  449. return -1;
  450. }
  451. }
  452. s->mb_intra = 0;
  453. if (HAS_CBP(mb_type)) {
  454. s->dsp.clear_blocks(s->block[0]);
  455. cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
  456. if(mb_block_count > 6){
  457. cbp<<= mb_block_count-6;
  458. cbp |= get_bits(&s->gb, mb_block_count-6);
  459. s->dsp.clear_blocks(s->block[6]);
  460. }
  461. if (cbp <= 0){
  462. av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
  463. return -1;
  464. }
  465. #ifdef HAVE_XVMC
  466. //on 1 we memcpy blocks in xvmcvideo
  467. if(s->avctx->xvmc_acceleration > 1){
  468. XVMC_pack_pblocks(s,cbp);
  469. if(s->swap_uv){
  470. exchange_uv(s);
  471. }
  472. }
  473. #endif
  474. if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
  475. if(s->flags2 & CODEC_FLAG2_FAST){
  476. for(i=0;i<6;i++) {
  477. if(cbp & 32) {
  478. mpeg2_fast_decode_block_non_intra(s, s->pblocks[i], i);
  479. } else {
  480. s->block_last_index[i] = -1;
  481. }
  482. cbp+=cbp;
  483. }
  484. }else{
  485. cbp<<= 12-mb_block_count;
  486. for(i=0;i<mb_block_count;i++) {
  487. if ( cbp & (1<<11) ) {
  488. if (mpeg2_decode_block_non_intra(s, s->pblocks[i], i) < 0)
  489. return -1;
  490. } else {
  491. s->block_last_index[i] = -1;
  492. }
  493. cbp+=cbp;
  494. }
  495. }
  496. } else {
  497. if(s->flags2 & CODEC_FLAG2_FAST){
  498. for(i=0;i<6;i++) {
  499. if (cbp & 32) {
  500. mpeg1_fast_decode_block_inter(s, s->pblocks[i], i);
  501. } else {
  502. s->block_last_index[i] = -1;
  503. }
  504. cbp+=cbp;
  505. }
  506. }else{
  507. for(i=0;i<6;i++) {
  508. if (cbp & 32) {
  509. if (mpeg1_decode_block_inter(s, s->pblocks[i], i) < 0)
  510. return -1;
  511. } else {
  512. s->block_last_index[i] = -1;
  513. }
  514. cbp+=cbp;
  515. }
  516. }
  517. }
  518. }else{
  519. for(i=0;i<12;i++)
  520. s->block_last_index[i] = -1;
  521. }
  522. }
  523. s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= mb_type;
  524. return 0;
  525. }
  526. /* as h263, but only 17 codes */
  527. static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
  528. {
  529. int code, sign, val, l, shift;
  530. code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
  531. if (code == 0) {
  532. return pred;
  533. }
  534. if (code < 0) {
  535. return 0xffff;
  536. }
  537. sign = get_bits1(&s->gb);
  538. shift = fcode - 1;
  539. val = code;
  540. if (shift) {
  541. val = (val - 1) << shift;
  542. val |= get_bits(&s->gb, shift);
  543. val++;
  544. }
  545. if (sign)
  546. val = -val;
  547. val += pred;
  548. /* modulo decoding */
  549. l= INT_BIT - 5 - shift;
  550. val = (val<<l)>>l;
  551. return val;
  552. }
  553. static inline int decode_dc(GetBitContext *gb, int component)
  554. {
  555. int code, diff;
  556. if (component == 0) {
  557. code = get_vlc2(gb, dc_lum_vlc.table, DC_VLC_BITS, 2);
  558. } else {
  559. code = get_vlc2(gb, dc_chroma_vlc.table, DC_VLC_BITS, 2);
  560. }
  561. if (code < 0){
  562. av_log(NULL, AV_LOG_ERROR, "invalid dc code at\n");
  563. return 0xffff;
  564. }
  565. if (code == 0) {
  566. diff = 0;
  567. } else {
  568. diff = get_xbits(gb, code);
  569. }
  570. return diff;
  571. }
  572. static inline int mpeg1_decode_block_intra(MpegEncContext *s,
  573. DCTELEM *block,
  574. int n)
  575. {
  576. int level, dc, diff, i, j, run;
  577. int component;
  578. RLTable *rl = &ff_rl_mpeg1;
  579. uint8_t * const scantable= s->intra_scantable.permutated;
  580. const uint16_t *quant_matrix= s->intra_matrix;
  581. const int qscale= s->qscale;
  582. /* DC coef */
  583. component = (n <= 3 ? 0 : n - 4 + 1);
  584. diff = decode_dc(&s->gb, component);
  585. if (diff >= 0xffff)
  586. return -1;
  587. dc = s->last_dc[component];
  588. dc += diff;
  589. s->last_dc[component] = dc;
  590. block[0] = dc<<3;
  591. dprintf(s->avctx, "dc=%d diff=%d\n", dc, diff);
  592. i = 0;
  593. {
  594. OPEN_READER(re, &s->gb);
  595. /* now quantify & encode AC coefs */
  596. for(;;) {
  597. UPDATE_CACHE(re, &s->gb);
  598. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  599. if(level == 127){
  600. break;
  601. } else if(level != 0) {
  602. i += run;
  603. j = scantable[i];
  604. level= (level*qscale*quant_matrix[j])>>4;
  605. level= (level-1)|1;
  606. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  607. LAST_SKIP_BITS(re, &s->gb, 1);
  608. } else {
  609. /* escape */
  610. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  611. UPDATE_CACHE(re, &s->gb);
  612. level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
  613. if (level == -128) {
  614. level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
  615. } else if (level == 0) {
  616. level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
  617. }
  618. i += run;
  619. j = scantable[i];
  620. if(level<0){
  621. level= -level;
  622. level= (level*qscale*quant_matrix[j])>>4;
  623. level= (level-1)|1;
  624. level= -level;
  625. }else{
  626. level= (level*qscale*quant_matrix[j])>>4;
  627. level= (level-1)|1;
  628. }
  629. }
  630. if (i > 63){
  631. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  632. return -1;
  633. }
  634. block[j] = level;
  635. }
  636. CLOSE_READER(re, &s->gb);
  637. }
  638. s->block_last_index[n] = i;
  639. return 0;
  640. }
  641. static inline int mpeg1_decode_block_inter(MpegEncContext *s,
  642. DCTELEM *block,
  643. int n)
  644. {
  645. int level, i, j, run;
  646. RLTable *rl = &ff_rl_mpeg1;
  647. uint8_t * const scantable= s->intra_scantable.permutated;
  648. const uint16_t *quant_matrix= s->inter_matrix;
  649. const int qscale= s->qscale;
  650. {
  651. OPEN_READER(re, &s->gb);
  652. i = -1;
  653. /* special case for the first coef. no need to add a second vlc table */
  654. UPDATE_CACHE(re, &s->gb);
  655. if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
  656. level= (3*qscale*quant_matrix[0])>>5;
  657. level= (level-1)|1;
  658. if(GET_CACHE(re, &s->gb)&0x40000000)
  659. level= -level;
  660. block[0] = level;
  661. i++;
  662. SKIP_BITS(re, &s->gb, 2);
  663. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  664. goto end;
  665. }
  666. #if MIN_CACHE_BITS < 19
  667. UPDATE_CACHE(re, &s->gb);
  668. #endif
  669. /* now quantify & encode AC coefs */
  670. for(;;) {
  671. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  672. if(level != 0) {
  673. i += run;
  674. j = scantable[i];
  675. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  676. level= (level-1)|1;
  677. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  678. SKIP_BITS(re, &s->gb, 1);
  679. } else {
  680. /* escape */
  681. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  682. UPDATE_CACHE(re, &s->gb);
  683. level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
  684. if (level == -128) {
  685. level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
  686. } else if (level == 0) {
  687. level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
  688. }
  689. i += run;
  690. j = scantable[i];
  691. if(level<0){
  692. level= -level;
  693. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  694. level= (level-1)|1;
  695. level= -level;
  696. }else{
  697. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  698. level= (level-1)|1;
  699. }
  700. }
  701. if (i > 63){
  702. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  703. return -1;
  704. }
  705. block[j] = level;
  706. #if MIN_CACHE_BITS < 19
  707. UPDATE_CACHE(re, &s->gb);
  708. #endif
  709. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  710. break;
  711. #if MIN_CACHE_BITS >= 19
  712. UPDATE_CACHE(re, &s->gb);
  713. #endif
  714. }
  715. end:
  716. LAST_SKIP_BITS(re, &s->gb, 2);
  717. CLOSE_READER(re, &s->gb);
  718. }
  719. s->block_last_index[n] = i;
  720. return 0;
  721. }
  722. static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
  723. {
  724. int level, i, j, run;
  725. RLTable *rl = &ff_rl_mpeg1;
  726. uint8_t * const scantable= s->intra_scantable.permutated;
  727. const int qscale= s->qscale;
  728. {
  729. OPEN_READER(re, &s->gb);
  730. i = -1;
  731. /* special case for the first coef. no need to add a second vlc table */
  732. UPDATE_CACHE(re, &s->gb);
  733. if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
  734. level= (3*qscale)>>1;
  735. level= (level-1)|1;
  736. if(GET_CACHE(re, &s->gb)&0x40000000)
  737. level= -level;
  738. block[0] = level;
  739. i++;
  740. SKIP_BITS(re, &s->gb, 2);
  741. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  742. goto end;
  743. }
  744. #if MIN_CACHE_BITS < 19
  745. UPDATE_CACHE(re, &s->gb);
  746. #endif
  747. /* now quantify & encode AC coefs */
  748. for(;;) {
  749. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  750. if(level != 0) {
  751. i += run;
  752. j = scantable[i];
  753. level= ((level*2+1)*qscale)>>1;
  754. level= (level-1)|1;
  755. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  756. SKIP_BITS(re, &s->gb, 1);
  757. } else {
  758. /* escape */
  759. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  760. UPDATE_CACHE(re, &s->gb);
  761. level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
  762. if (level == -128) {
  763. level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
  764. } else if (level == 0) {
  765. level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
  766. }
  767. i += run;
  768. j = scantable[i];
  769. if(level<0){
  770. level= -level;
  771. level= ((level*2+1)*qscale)>>1;
  772. level= (level-1)|1;
  773. level= -level;
  774. }else{
  775. level= ((level*2+1)*qscale)>>1;
  776. level= (level-1)|1;
  777. }
  778. }
  779. block[j] = level;
  780. #if MIN_CACHE_BITS < 19
  781. UPDATE_CACHE(re, &s->gb);
  782. #endif
  783. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  784. break;
  785. #if MIN_CACHE_BITS >= 19
  786. UPDATE_CACHE(re, &s->gb);
  787. #endif
  788. }
  789. end:
  790. LAST_SKIP_BITS(re, &s->gb, 2);
  791. CLOSE_READER(re, &s->gb);
  792. }
  793. s->block_last_index[n] = i;
  794. return 0;
  795. }
  796. static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
  797. DCTELEM *block,
  798. int n)
  799. {
  800. int level, i, j, run;
  801. RLTable *rl = &ff_rl_mpeg1;
  802. uint8_t * const scantable= s->intra_scantable.permutated;
  803. const uint16_t *quant_matrix;
  804. const int qscale= s->qscale;
  805. int mismatch;
  806. mismatch = 1;
  807. {
  808. OPEN_READER(re, &s->gb);
  809. i = -1;
  810. if (n < 4)
  811. quant_matrix = s->inter_matrix;
  812. else
  813. quant_matrix = s->chroma_inter_matrix;
  814. /* special case for the first coef. no need to add a second vlc table */
  815. UPDATE_CACHE(re, &s->gb);
  816. if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
  817. level= (3*qscale*quant_matrix[0])>>5;
  818. if(GET_CACHE(re, &s->gb)&0x40000000)
  819. level= -level;
  820. block[0] = level;
  821. mismatch ^= level;
  822. i++;
  823. SKIP_BITS(re, &s->gb, 2);
  824. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  825. goto end;
  826. }
  827. #if MIN_CACHE_BITS < 19
  828. UPDATE_CACHE(re, &s->gb);
  829. #endif
  830. /* now quantify & encode AC coefs */
  831. for(;;) {
  832. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  833. if(level != 0) {
  834. i += run;
  835. j = scantable[i];
  836. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  837. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  838. SKIP_BITS(re, &s->gb, 1);
  839. } else {
  840. /* escape */
  841. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  842. UPDATE_CACHE(re, &s->gb);
  843. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  844. i += run;
  845. j = scantable[i];
  846. if(level<0){
  847. level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
  848. level= -level;
  849. }else{
  850. level= ((level*2+1)*qscale*quant_matrix[j])>>5;
  851. }
  852. }
  853. if (i > 63){
  854. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  855. return -1;
  856. }
  857. mismatch ^= level;
  858. block[j] = level;
  859. #if MIN_CACHE_BITS < 19
  860. UPDATE_CACHE(re, &s->gb);
  861. #endif
  862. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  863. break;
  864. #if MIN_CACHE_BITS >= 19
  865. UPDATE_CACHE(re, &s->gb);
  866. #endif
  867. }
  868. end:
  869. LAST_SKIP_BITS(re, &s->gb, 2);
  870. CLOSE_READER(re, &s->gb);
  871. }
  872. block[63] ^= (mismatch & 1);
  873. s->block_last_index[n] = i;
  874. return 0;
  875. }
  876. static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
  877. DCTELEM *block,
  878. int n)
  879. {
  880. int level, i, j, run;
  881. RLTable *rl = &ff_rl_mpeg1;
  882. uint8_t * const scantable= s->intra_scantable.permutated;
  883. const int qscale= s->qscale;
  884. OPEN_READER(re, &s->gb);
  885. i = -1;
  886. /* special case for the first coef. no need to add a second vlc table */
  887. UPDATE_CACHE(re, &s->gb);
  888. if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
  889. level= (3*qscale)>>1;
  890. if(GET_CACHE(re, &s->gb)&0x40000000)
  891. level= -level;
  892. block[0] = level;
  893. i++;
  894. SKIP_BITS(re, &s->gb, 2);
  895. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  896. goto end;
  897. }
  898. #if MIN_CACHE_BITS < 19
  899. UPDATE_CACHE(re, &s->gb);
  900. #endif
  901. /* now quantify & encode AC coefs */
  902. for(;;) {
  903. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  904. if(level != 0) {
  905. i += run;
  906. j = scantable[i];
  907. level= ((level*2+1)*qscale)>>1;
  908. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  909. SKIP_BITS(re, &s->gb, 1);
  910. } else {
  911. /* escape */
  912. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  913. UPDATE_CACHE(re, &s->gb);
  914. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  915. i += run;
  916. j = scantable[i];
  917. if(level<0){
  918. level= ((-level*2+1)*qscale)>>1;
  919. level= -level;
  920. }else{
  921. level= ((level*2+1)*qscale)>>1;
  922. }
  923. }
  924. block[j] = level;
  925. #if MIN_CACHE_BITS < 19
  926. UPDATE_CACHE(re, &s->gb);
  927. #endif
  928. if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  929. break;
  930. #if MIN_CACHE_BITS >=19
  931. UPDATE_CACHE(re, &s->gb);
  932. #endif
  933. }
  934. end:
  935. LAST_SKIP_BITS(re, &s->gb, 2);
  936. CLOSE_READER(re, &s->gb);
  937. s->block_last_index[n] = i;
  938. return 0;
  939. }
  940. static inline int mpeg2_decode_block_intra(MpegEncContext *s,
  941. DCTELEM *block,
  942. int n)
  943. {
  944. int level, dc, diff, i, j, run;
  945. int component;
  946. RLTable *rl;
  947. uint8_t * const scantable= s->intra_scantable.permutated;
  948. const uint16_t *quant_matrix;
  949. const int qscale= s->qscale;
  950. int mismatch;
  951. /* DC coef */
  952. if (n < 4){
  953. quant_matrix = s->intra_matrix;
  954. component = 0;
  955. }else{
  956. quant_matrix = s->chroma_intra_matrix;
  957. component = (n&1) + 1;
  958. }
  959. diff = decode_dc(&s->gb, component);
  960. if (diff >= 0xffff)
  961. return -1;
  962. dc = s->last_dc[component];
  963. dc += diff;
  964. s->last_dc[component] = dc;
  965. block[0] = dc << (3 - s->intra_dc_precision);
  966. dprintf(s->avctx, "dc=%d\n", block[0]);
  967. mismatch = block[0] ^ 1;
  968. i = 0;
  969. if (s->intra_vlc_format)
  970. rl = &ff_rl_mpeg2;
  971. else
  972. rl = &ff_rl_mpeg1;
  973. {
  974. OPEN_READER(re, &s->gb);
  975. /* now quantify & encode AC coefs */
  976. for(;;) {
  977. UPDATE_CACHE(re, &s->gb);
  978. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  979. if(level == 127){
  980. break;
  981. } else if(level != 0) {
  982. i += run;
  983. j = scantable[i];
  984. level= (level*qscale*quant_matrix[j])>>4;
  985. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  986. LAST_SKIP_BITS(re, &s->gb, 1);
  987. } else {
  988. /* escape */
  989. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  990. UPDATE_CACHE(re, &s->gb);
  991. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  992. i += run;
  993. j = scantable[i];
  994. if(level<0){
  995. level= (-level*qscale*quant_matrix[j])>>4;
  996. level= -level;
  997. }else{
  998. level= (level*qscale*quant_matrix[j])>>4;
  999. }
  1000. }
  1001. if (i > 63){
  1002. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  1003. return -1;
  1004. }
  1005. mismatch^= level;
  1006. block[j] = level;
  1007. }
  1008. CLOSE_READER(re, &s->gb);
  1009. }
  1010. block[63]^= mismatch&1;
  1011. s->block_last_index[n] = i;
  1012. return 0;
  1013. }
  1014. static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
  1015. DCTELEM *block,
  1016. int n)
  1017. {
  1018. int level, dc, diff, j, run;
  1019. int component;
  1020. RLTable *rl;
  1021. uint8_t * scantable= s->intra_scantable.permutated;
  1022. const uint16_t *quant_matrix;
  1023. const int qscale= s->qscale;
  1024. /* DC coef */
  1025. if (n < 4){
  1026. quant_matrix = s->intra_matrix;
  1027. component = 0;
  1028. }else{
  1029. quant_matrix = s->chroma_intra_matrix;
  1030. component = (n&1) + 1;
  1031. }
  1032. diff = decode_dc(&s->gb, component);
  1033. if (diff >= 0xffff)
  1034. return -1;
  1035. dc = s->last_dc[component];
  1036. dc += diff;
  1037. s->last_dc[component] = dc;
  1038. block[0] = dc << (3 - s->intra_dc_precision);
  1039. if (s->intra_vlc_format)
  1040. rl = &ff_rl_mpeg2;
  1041. else
  1042. rl = &ff_rl_mpeg1;
  1043. {
  1044. OPEN_READER(re, &s->gb);
  1045. /* now quantify & encode AC coefs */
  1046. for(;;) {
  1047. UPDATE_CACHE(re, &s->gb);
  1048. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  1049. if(level == 127){
  1050. break;
  1051. } else if(level != 0) {
  1052. scantable += run;
  1053. j = *scantable;
  1054. level= (level*qscale*quant_matrix[j])>>4;
  1055. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  1056. LAST_SKIP_BITS(re, &s->gb, 1);
  1057. } else {
  1058. /* escape */
  1059. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  1060. UPDATE_CACHE(re, &s->gb);
  1061. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  1062. scantable += run;
  1063. j = *scantable;
  1064. if(level<0){
  1065. level= (-level*qscale*quant_matrix[j])>>4;
  1066. level= -level;
  1067. }else{
  1068. level= (level*qscale*quant_matrix[j])>>4;
  1069. }
  1070. }
  1071. block[j] = level;
  1072. }
  1073. CLOSE_READER(re, &s->gb);
  1074. }
  1075. s->block_last_index[n] = scantable - s->intra_scantable.permutated;
  1076. return 0;
  1077. }
  1078. typedef struct Mpeg1Context {
  1079. MpegEncContext mpeg_enc_ctx;
  1080. int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
  1081. int repeat_field; /* true if we must repeat the field */
  1082. AVPanScan pan_scan; /** some temporary storage for the panscan */
  1083. int slice_count;
  1084. int swap_uv;//indicate VCR2
  1085. int save_aspect_info;
  1086. int save_width, save_height;
  1087. AVRational frame_rate_ext; ///< MPEG-2 specific framerate modificator
  1088. } Mpeg1Context;
  1089. static av_cold int mpeg_decode_init(AVCodecContext *avctx)
  1090. {
  1091. Mpeg1Context *s = avctx->priv_data;
  1092. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  1093. int i;
  1094. //we need some parmutation to store
  1095. //matrixes, until MPV_common_init()
  1096. //set the real permutatuon
  1097. for(i=0;i<64;i++)
  1098. s2->dsp.idct_permutation[i]=i;
  1099. MPV_decode_defaults(s2);
  1100. s->mpeg_enc_ctx.avctx= avctx;
  1101. s->mpeg_enc_ctx.flags= avctx->flags;
  1102. s->mpeg_enc_ctx.flags2= avctx->flags2;
  1103. ff_mpeg12_common_init(&s->mpeg_enc_ctx);
  1104. init_vlcs();
  1105. s->mpeg_enc_ctx_allocated = 0;
  1106. s->mpeg_enc_ctx.picture_number = 0;
  1107. s->repeat_field = 0;
  1108. s->mpeg_enc_ctx.codec_id= avctx->codec->id;
  1109. return 0;
  1110. }
  1111. static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
  1112. const uint8_t *new_perm){
  1113. uint16_t temp_matrix[64];
  1114. int i;
  1115. memcpy(temp_matrix,matrix,64*sizeof(uint16_t));
  1116. for(i=0;i<64;i++){
  1117. matrix[new_perm[i]] = temp_matrix[old_perm[i]];
  1118. }
  1119. }
  1120. //Call this function when we know all parameters
  1121. //it may be called in different places for mpeg1 and mpeg2
  1122. static int mpeg_decode_postinit(AVCodecContext *avctx){
  1123. Mpeg1Context *s1 = avctx->priv_data;
  1124. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1125. uint8_t old_permutation[64];
  1126. if (
  1127. (s1->mpeg_enc_ctx_allocated == 0)||
  1128. avctx->coded_width != s->width ||
  1129. avctx->coded_height != s->height||
  1130. s1->save_width != s->width ||
  1131. s1->save_height != s->height ||
  1132. s1->save_aspect_info != s->aspect_ratio_info||
  1133. 0)
  1134. {
  1135. if (s1->mpeg_enc_ctx_allocated) {
  1136. ParseContext pc= s->parse_context;
  1137. s->parse_context.buffer=0;
  1138. MPV_common_end(s);
  1139. s->parse_context= pc;
  1140. }
  1141. if( (s->width == 0 )||(s->height == 0))
  1142. return -2;
  1143. avcodec_set_dimensions(avctx, s->width, s->height);
  1144. avctx->bit_rate = s->bit_rate;
  1145. s1->save_aspect_info = s->aspect_ratio_info;
  1146. s1->save_width = s->width;
  1147. s1->save_height = s->height;
  1148. //low_delay may be forced, in this case we will have B frames
  1149. //that behave like P frames
  1150. avctx->has_b_frames = !(s->low_delay);
  1151. if(avctx->sub_id==1){//s->codec_id==avctx->codec_id==CODEC_ID
  1152. //mpeg1 fps
  1153. avctx->time_base.den= ff_frame_rate_tab[s->frame_rate_index].num;
  1154. avctx->time_base.num= ff_frame_rate_tab[s->frame_rate_index].den;
  1155. //mpeg1 aspect
  1156. avctx->sample_aspect_ratio= av_d2q(
  1157. 1.0/ff_mpeg1_aspect[s->aspect_ratio_info], 255);
  1158. }else{//mpeg2
  1159. //mpeg2 fps
  1160. av_reduce(
  1161. &s->avctx->time_base.den,
  1162. &s->avctx->time_base.num,
  1163. ff_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num,
  1164. ff_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
  1165. 1<<30);
  1166. //mpeg2 aspect
  1167. if(s->aspect_ratio_info > 1){
  1168. if( (s1->pan_scan.width == 0 )||(s1->pan_scan.height == 0) ){
  1169. s->avctx->sample_aspect_ratio=
  1170. av_div_q(
  1171. ff_mpeg2_aspect[s->aspect_ratio_info],
  1172. (AVRational){s->width, s->height}
  1173. );
  1174. }else{
  1175. s->avctx->sample_aspect_ratio=
  1176. av_div_q(
  1177. ff_mpeg2_aspect[s->aspect_ratio_info],
  1178. (AVRational){s1->pan_scan.width, s1->pan_scan.height}
  1179. );
  1180. }
  1181. }else{
  1182. s->avctx->sample_aspect_ratio=
  1183. ff_mpeg2_aspect[s->aspect_ratio_info];
  1184. }
  1185. }//mpeg2
  1186. if(avctx->xvmc_acceleration){
  1187. avctx->pix_fmt = avctx->get_format(avctx,pixfmt_xvmc_mpg2_420);
  1188. }else{
  1189. if(s->chroma_format < 2){
  1190. avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_420);
  1191. }else
  1192. if(s->chroma_format == 2){
  1193. avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_422);
  1194. }else
  1195. if(s->chroma_format > 2){
  1196. avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_444);
  1197. }
  1198. }
  1199. //until then pix_fmt may be changed right after codec init
  1200. if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT )
  1201. if( avctx->idct_algo == FF_IDCT_AUTO )
  1202. avctx->idct_algo = FF_IDCT_SIMPLE;
  1203. //quantization matrixes may need reordering
  1204. //if dct permutation is changed
  1205. memcpy(old_permutation,s->dsp.idct_permutation,64*sizeof(uint8_t));
  1206. if (MPV_common_init(s) < 0)
  1207. return -2;
  1208. quant_matrix_rebuild(s->intra_matrix, old_permutation,s->dsp.idct_permutation);
  1209. quant_matrix_rebuild(s->inter_matrix, old_permutation,s->dsp.idct_permutation);
  1210. quant_matrix_rebuild(s->chroma_intra_matrix,old_permutation,s->dsp.idct_permutation);
  1211. quant_matrix_rebuild(s->chroma_inter_matrix,old_permutation,s->dsp.idct_permutation);
  1212. s1->mpeg_enc_ctx_allocated = 1;
  1213. }
  1214. return 0;
  1215. }
  1216. static int mpeg1_decode_picture(AVCodecContext *avctx,
  1217. const uint8_t *buf, int buf_size)
  1218. {
  1219. Mpeg1Context *s1 = avctx->priv_data;
  1220. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1221. int ref, f_code, vbv_delay;
  1222. if(mpeg_decode_postinit(s->avctx) < 0)
  1223. return -2;
  1224. init_get_bits(&s->gb, buf, buf_size*8);
  1225. ref = get_bits(&s->gb, 10); /* temporal ref */
  1226. s->pict_type = get_bits(&s->gb, 3);
  1227. if(s->pict_type == 0 || s->pict_type > 3)
  1228. return -1;
  1229. vbv_delay= get_bits(&s->gb, 16);
  1230. if (s->pict_type == FF_P_TYPE || s->pict_type == FF_B_TYPE) {
  1231. s->full_pel[0] = get_bits1(&s->gb);
  1232. f_code = get_bits(&s->gb, 3);
  1233. if (f_code == 0 && avctx->error_resilience >= FF_ER_COMPLIANT)
  1234. return -1;
  1235. s->mpeg_f_code[0][0] = f_code;
  1236. s->mpeg_f_code[0][1] = f_code;
  1237. }
  1238. if (s->pict_type == FF_B_TYPE) {
  1239. s->full_pel[1] = get_bits1(&s->gb);
  1240. f_code = get_bits(&s->gb, 3);
  1241. if (f_code == 0 && avctx->error_resilience >= FF_ER_COMPLIANT)
  1242. return -1;
  1243. s->mpeg_f_code[1][0] = f_code;
  1244. s->mpeg_f_code[1][1] = f_code;
  1245. }
  1246. s->current_picture.pict_type= s->pict_type;
  1247. s->current_picture.key_frame= s->pict_type == FF_I_TYPE;
  1248. if(avctx->debug & FF_DEBUG_PICT_INFO)
  1249. av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
  1250. s->y_dc_scale = 8;
  1251. s->c_dc_scale = 8;
  1252. s->first_slice = 1;
  1253. return 0;
  1254. }
  1255. static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
  1256. {
  1257. MpegEncContext *s= &s1->mpeg_enc_ctx;
  1258. int horiz_size_ext, vert_size_ext;
  1259. int bit_rate_ext;
  1260. skip_bits(&s->gb, 1); /* profil and level esc*/
  1261. s->avctx->profile= get_bits(&s->gb, 3);
  1262. s->avctx->level= get_bits(&s->gb, 4);
  1263. s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
  1264. s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
  1265. horiz_size_ext = get_bits(&s->gb, 2);
  1266. vert_size_ext = get_bits(&s->gb, 2);
  1267. s->width |= (horiz_size_ext << 12);
  1268. s->height |= (vert_size_ext << 12);
  1269. bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
  1270. s->bit_rate += (bit_rate_ext << 18) * 400;
  1271. skip_bits1(&s->gb); /* marker */
  1272. s->avctx->rc_buffer_size += get_bits(&s->gb, 8)*1024*16<<10;
  1273. s->low_delay = get_bits1(&s->gb);
  1274. if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
  1275. s1->frame_rate_ext.num = get_bits(&s->gb, 2)+1;
  1276. s1->frame_rate_ext.den = get_bits(&s->gb, 5)+1;
  1277. dprintf(s->avctx, "sequence extension\n");
  1278. s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
  1279. s->avctx->sub_id = 2; /* indicates mpeg2 found */
  1280. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1281. av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
  1282. s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
  1283. }
  1284. static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
  1285. {
  1286. MpegEncContext *s= &s1->mpeg_enc_ctx;
  1287. int color_description, w, h;
  1288. skip_bits(&s->gb, 3); /* video format */
  1289. color_description= get_bits1(&s->gb);
  1290. if(color_description){
  1291. skip_bits(&s->gb, 8); /* color primaries */
  1292. skip_bits(&s->gb, 8); /* transfer_characteristics */
  1293. skip_bits(&s->gb, 8); /* matrix_coefficients */
  1294. }
  1295. w= get_bits(&s->gb, 14);
  1296. skip_bits(&s->gb, 1); //marker
  1297. h= get_bits(&s->gb, 14);
  1298. skip_bits(&s->gb, 1); //marker
  1299. s1->pan_scan.width= 16*w;
  1300. s1->pan_scan.height=16*h;
  1301. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1302. av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
  1303. }
  1304. static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
  1305. {
  1306. MpegEncContext *s= &s1->mpeg_enc_ctx;
  1307. int i,nofco;
  1308. nofco = 1;
  1309. if(s->progressive_sequence){
  1310. if(s->repeat_first_field){
  1311. nofco++;
  1312. if(s->top_field_first)
  1313. nofco++;
  1314. }
  1315. }else{
  1316. if(s->picture_structure == PICT_FRAME){
  1317. nofco++;
  1318. if(s->repeat_first_field)
  1319. nofco++;
  1320. }
  1321. }
  1322. for(i=0; i<nofco; i++){
  1323. s1->pan_scan.position[i][0]= get_sbits(&s->gb, 16);
  1324. skip_bits(&s->gb, 1); //marker
  1325. s1->pan_scan.position[i][1]= get_sbits(&s->gb, 16);
  1326. skip_bits(&s->gb, 1); //marker
  1327. }
  1328. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1329. av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
  1330. s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
  1331. s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
  1332. s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]
  1333. );
  1334. }
  1335. static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
  1336. {
  1337. int i, v, j;
  1338. dprintf(s->avctx, "matrix extension\n");
  1339. if (get_bits1(&s->gb)) {
  1340. for(i=0;i<64;i++) {
  1341. v = get_bits(&s->gb, 8);
  1342. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  1343. s->intra_matrix[j] = v;
  1344. s->chroma_intra_matrix[j] = v;
  1345. }
  1346. }
  1347. if (get_bits1(&s->gb)) {
  1348. for(i=0;i<64;i++) {
  1349. v = get_bits(&s->gb, 8);
  1350. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  1351. s->inter_matrix[j] = v;
  1352. s->chroma_inter_matrix[j] = v;
  1353. }
  1354. }
  1355. if (get_bits1(&s->gb)) {
  1356. for(i=0;i<64;i++) {
  1357. v = get_bits(&s->gb, 8);
  1358. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  1359. s->chroma_intra_matrix[j] = v;
  1360. }
  1361. }
  1362. if (get_bits1(&s->gb)) {
  1363. for(i=0;i<64;i++) {
  1364. v = get_bits(&s->gb, 8);
  1365. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  1366. s->chroma_inter_matrix[j] = v;
  1367. }
  1368. }
  1369. }
  1370. static void mpeg_decode_picture_coding_extension(MpegEncContext *s)
  1371. {
  1372. s->full_pel[0] = s->full_pel[1] = 0;
  1373. s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
  1374. s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
  1375. s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
  1376. s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
  1377. s->intra_dc_precision = get_bits(&s->gb, 2);
  1378. s->picture_structure = get_bits(&s->gb, 2);
  1379. s->top_field_first = get_bits1(&s->gb);
  1380. s->frame_pred_frame_dct = get_bits1(&s->gb);
  1381. s->concealment_motion_vectors = get_bits1(&s->gb);
  1382. s->q_scale_type = get_bits1(&s->gb);
  1383. s->intra_vlc_format = get_bits1(&s->gb);
  1384. s->alternate_scan = get_bits1(&s->gb);
  1385. s->repeat_first_field = get_bits1(&s->gb);
  1386. s->chroma_420_type = get_bits1(&s->gb);
  1387. s->progressive_frame = get_bits1(&s->gb);
  1388. if(s->picture_structure == PICT_FRAME){
  1389. s->first_field=0;
  1390. s->v_edge_pos= 16*s->mb_height;
  1391. }else{
  1392. s->first_field ^= 1;
  1393. s->v_edge_pos= 8*s->mb_height;
  1394. memset(s->mbskip_table, 0, s->mb_stride*s->mb_height);
  1395. }
  1396. if(s->alternate_scan){
  1397. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
  1398. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
  1399. }else{
  1400. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
  1401. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
  1402. }
  1403. /* composite display not parsed */
  1404. dprintf(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
  1405. dprintf(s->avctx, "picture_structure=%d\n", s->picture_structure);
  1406. dprintf(s->avctx, "top field first=%d\n", s->top_field_first);
  1407. dprintf(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
  1408. dprintf(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
  1409. dprintf(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
  1410. dprintf(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
  1411. dprintf(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
  1412. dprintf(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
  1413. }
  1414. static void mpeg_decode_extension(AVCodecContext *avctx,
  1415. const uint8_t *buf, int buf_size)
  1416. {
  1417. Mpeg1Context *s1 = avctx->priv_data;
  1418. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1419. int ext_type;
  1420. init_get_bits(&s->gb, buf, buf_size*8);
  1421. ext_type = get_bits(&s->gb, 4);
  1422. switch(ext_type) {
  1423. case 0x1:
  1424. mpeg_decode_sequence_extension(s1);
  1425. break;
  1426. case 0x2:
  1427. mpeg_decode_sequence_display_extension(s1);
  1428. break;
  1429. case 0x3:
  1430. mpeg_decode_quant_matrix_extension(s);
  1431. break;
  1432. case 0x7:
  1433. mpeg_decode_picture_display_extension(s1);
  1434. break;
  1435. case 0x8:
  1436. mpeg_decode_picture_coding_extension(s);
  1437. break;
  1438. }
  1439. }
  1440. static void exchange_uv(MpegEncContext *s){
  1441. short * tmp = s->pblocks[4];
  1442. s->pblocks[4] = s->pblocks[5];
  1443. s->pblocks[5] = tmp;
  1444. }
  1445. static int mpeg_field_start(MpegEncContext *s){
  1446. AVCodecContext *avctx= s->avctx;
  1447. Mpeg1Context *s1 = (Mpeg1Context*)s;
  1448. /* start frame decoding */
  1449. if(s->first_field || s->picture_structure==PICT_FRAME){
  1450. if(MPV_frame_start(s, avctx) < 0)
  1451. return -1;
  1452. ff_er_frame_start(s);
  1453. /* first check if we must repeat the frame */
  1454. s->current_picture_ptr->repeat_pict = 0;
  1455. if (s->repeat_first_field) {
  1456. if (s->progressive_sequence) {
  1457. if (s->top_field_first)
  1458. s->current_picture_ptr->repeat_pict = 4;
  1459. else
  1460. s->current_picture_ptr->repeat_pict = 2;
  1461. } else if (s->progressive_frame) {
  1462. s->current_picture_ptr->repeat_pict = 1;
  1463. }
  1464. }
  1465. *s->current_picture_ptr->pan_scan= s1->pan_scan;
  1466. }else{ //second field
  1467. int i;
  1468. if(!s->current_picture_ptr){
  1469. av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
  1470. return -1;
  1471. }
  1472. for(i=0; i<4; i++){
  1473. s->current_picture.data[i] = s->current_picture_ptr->data[i];
  1474. if(s->picture_structure == PICT_BOTTOM_FIELD){
  1475. s->current_picture.data[i] += s->current_picture_ptr->linesize[i];
  1476. }
  1477. }
  1478. }
  1479. #ifdef HAVE_XVMC
  1480. // MPV_frame_start will call this function too,
  1481. // but we need to call it on every field
  1482. if(s->avctx->xvmc_acceleration)
  1483. XVMC_field_start(s,avctx);
  1484. #endif
  1485. return 0;
  1486. }
  1487. #define DECODE_SLICE_ERROR -1
  1488. #define DECODE_SLICE_OK 0
  1489. /**
  1490. * decodes a slice. MpegEncContext.mb_y must be set to the MB row from the startcode
  1491. * @return DECODE_SLICE_ERROR if the slice is damaged<br>
  1492. * DECODE_SLICE_OK if this slice is ok<br>
  1493. */
  1494. static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y,
  1495. const uint8_t **buf, int buf_size)
  1496. {
  1497. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1498. AVCodecContext *avctx= s->avctx;
  1499. const int field_pic= s->picture_structure != PICT_FRAME;
  1500. const int lowres= s->avctx->lowres;
  1501. s->resync_mb_x=
  1502. s->resync_mb_y= -1;
  1503. if (mb_y<<field_pic >= s->mb_height){
  1504. av_log(s->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s->mb_height);
  1505. return -1;
  1506. }
  1507. init_get_bits(&s->gb, *buf, buf_size*8);
  1508. ff_mpeg1_clean_buffers(s);
  1509. s->interlaced_dct = 0;
  1510. s->qscale = get_qscale(s);
  1511. if(s->qscale == 0){
  1512. av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
  1513. return -1;
  1514. }
  1515. /* extra slice info */
  1516. while (get_bits1(&s->gb) != 0) {
  1517. skip_bits(&s->gb, 8);
  1518. }
  1519. s->mb_x=0;
  1520. for(;;) {
  1521. int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  1522. if (code < 0){
  1523. av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
  1524. return -1;
  1525. }
  1526. if (code >= 33) {
  1527. if (code == 33) {
  1528. s->mb_x += 33;
  1529. }
  1530. /* otherwise, stuffing, nothing to do */
  1531. } else {
  1532. s->mb_x += code;
  1533. break;
  1534. }
  1535. }
  1536. if(s->mb_x >= (unsigned)s->mb_width){
  1537. av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
  1538. return -1;
  1539. }
  1540. s->resync_mb_x= s->mb_x;
  1541. s->resync_mb_y= s->mb_y= mb_y;
  1542. s->mb_skip_run= 0;
  1543. ff_init_block_index(s);
  1544. if (s->mb_y==0 && s->mb_x==0 && (s->first_field || s->picture_structure==PICT_FRAME)) {
  1545. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  1546. 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",
  1547. 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],
  1548. s->pict_type == FF_I_TYPE ? "I" : (s->pict_type == FF_P_TYPE ? "P" : (s->pict_type == FF_B_TYPE ? "B" : "S")),
  1549. s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
  1550. s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
  1551. s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
  1552. }
  1553. }
  1554. for(;;) {
  1555. #ifdef HAVE_XVMC
  1556. //one 1 we memcpy blocks in xvmcvideo
  1557. if(s->avctx->xvmc_acceleration > 1)
  1558. XVMC_init_block(s);//set s->block
  1559. #endif
  1560. if(mpeg_decode_mb(s, s->block) < 0)
  1561. return -1;
  1562. if(s->current_picture.motion_val[0] && !s->encoding){ //note motion_val is normally NULL unless we want to extract the MVs
  1563. const int wrap = field_pic ? 2*s->b8_stride : s->b8_stride;
  1564. int xy = s->mb_x*2 + s->mb_y*2*wrap;
  1565. int motion_x, motion_y, dir, i;
  1566. if(field_pic && !s->first_field)
  1567. xy += wrap/2;
  1568. for(i=0; i<2; i++){
  1569. for(dir=0; dir<2; dir++){
  1570. if (s->mb_intra || (dir==1 && s->pict_type != FF_B_TYPE)) {
  1571. motion_x = motion_y = 0;
  1572. }else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)){
  1573. motion_x = s->mv[dir][0][0];
  1574. motion_y = s->mv[dir][0][1];
  1575. } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
  1576. motion_x = s->mv[dir][i][0];
  1577. motion_y = s->mv[dir][i][1];
  1578. }
  1579. s->current_picture.motion_val[dir][xy ][0] = motion_x;
  1580. s->current_picture.motion_val[dir][xy ][1] = motion_y;
  1581. s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
  1582. s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
  1583. s->current_picture.ref_index [dir][xy ]=
  1584. s->current_picture.ref_index [dir][xy + 1]= s->field_select[dir][i];
  1585. assert(s->field_select[dir][i]==0 || s->field_select[dir][i]==1);
  1586. }
  1587. xy += wrap;
  1588. }
  1589. }
  1590. s->dest[0] += 16 >> lowres;
  1591. s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
  1592. s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
  1593. MPV_decode_mb(s, s->block);
  1594. if (++s->mb_x >= s->mb_width) {
  1595. const int mb_size= 16>>s->avctx->lowres;
  1596. ff_draw_horiz_band(s, mb_size*s->mb_y, mb_size);
  1597. s->mb_x = 0;
  1598. s->mb_y++;
  1599. if(s->mb_y<<field_pic >= s->mb_height){
  1600. int left= s->gb.size_in_bits - get_bits_count(&s->gb);
  1601. int is_d10= s->chroma_format==2 && s->pict_type==FF_I_TYPE && avctx->profile==0 && avctx->level==5
  1602. && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
  1603. && s->progressive_frame == 0 /* vbv_delay == 0xBBB || 0xE10*/;
  1604. if(left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
  1605. || (avctx->error_resilience >= FF_ER_AGGRESSIVE && left>8)){
  1606. av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
  1607. return -1;
  1608. }else
  1609. goto eos;
  1610. }
  1611. ff_init_block_index(s);
  1612. }
  1613. /* skip mb handling */
  1614. if (s->mb_skip_run == -1) {
  1615. /* read again increment */
  1616. s->mb_skip_run = 0;
  1617. for(;;) {
  1618. int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  1619. if (code < 0){
  1620. av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
  1621. return -1;
  1622. }
  1623. if (code >= 33) {
  1624. if (code == 33) {
  1625. s->mb_skip_run += 33;
  1626. }else if(code == 35){
  1627. if(s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0){
  1628. av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
  1629. return -1;
  1630. }
  1631. goto eos; /* end of slice */
  1632. }
  1633. /* otherwise, stuffing, nothing to do */
  1634. } else {
  1635. s->mb_skip_run += code;
  1636. break;
  1637. }
  1638. }
  1639. if(s->mb_skip_run){
  1640. int i;
  1641. if(s->pict_type == FF_I_TYPE){
  1642. av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
  1643. return -1;
  1644. }
  1645. /* skip mb */
  1646. s->mb_intra = 0;
  1647. for(i=0;i<12;i++)
  1648. s->block_last_index[i] = -1;
  1649. if(s->picture_structure == PICT_FRAME)
  1650. s->mv_type = MV_TYPE_16X16;
  1651. else
  1652. s->mv_type = MV_TYPE_FIELD;
  1653. if (s->pict_type == FF_P_TYPE) {
  1654. /* if P type, zero motion vector is implied */
  1655. s->mv_dir = MV_DIR_FORWARD;
  1656. s->mv[0][0][0] = s->mv[0][0][1] = 0;
  1657. s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
  1658. s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
  1659. s->field_select[0][0]= s->picture_structure - 1;
  1660. } else {
  1661. /* if B type, reuse previous vectors and directions */
  1662. s->mv[0][0][0] = s->last_mv[0][0][0];
  1663. s->mv[0][0][1] = s->last_mv[0][0][1];
  1664. s->mv[1][0][0] = s->last_mv[1][0][0];
  1665. s->mv[1][0][1] = s->last_mv[1][0][1];
  1666. }
  1667. }
  1668. }
  1669. }
  1670. eos: // end of slice
  1671. *buf += (get_bits_count(&s->gb)-1)/8;
  1672. //printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
  1673. return 0;
  1674. }
  1675. static int slice_decode_thread(AVCodecContext *c, void *arg){
  1676. MpegEncContext *s= arg;
  1677. const uint8_t *buf= s->gb.buffer;
  1678. int mb_y= s->start_mb_y;
  1679. s->error_count= 3*(s->end_mb_y - s->start_mb_y)*s->mb_width;
  1680. for(;;){
  1681. uint32_t start_code;
  1682. int ret;
  1683. ret= mpeg_decode_slice((Mpeg1Context*)s, mb_y, &buf, s->gb.buffer_end - buf);
  1684. emms_c();
  1685. //av_log(c, AV_LOG_DEBUG, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
  1686. //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);
  1687. if(ret < 0){
  1688. if(s->resync_mb_x>=0 && s->resync_mb_y>=0)
  1689. 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);
  1690. }else{
  1691. 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);
  1692. }
  1693. if(s->mb_y == s->end_mb_y)
  1694. return 0;
  1695. start_code= -1;
  1696. buf = ff_find_start_code(buf, s->gb.buffer_end, &start_code);
  1697. mb_y= start_code - SLICE_MIN_START_CODE;
  1698. if(mb_y < 0 || mb_y >= s->end_mb_y)
  1699. return -1;
  1700. }
  1701. return 0; //not reached
  1702. }
  1703. /**
  1704. * handles slice ends.
  1705. * @return 1 if it seems to be the last slice of
  1706. */
  1707. static int slice_end(AVCodecContext *avctx, AVFrame *pict)
  1708. {
  1709. Mpeg1Context *s1 = avctx->priv_data;
  1710. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1711. if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
  1712. return 0;
  1713. #ifdef HAVE_XVMC
  1714. if(s->avctx->xvmc_acceleration)
  1715. XVMC_field_end(s);
  1716. #endif
  1717. /* end of slice reached */
  1718. if (/*s->mb_y<<field_pic == s->mb_height &&*/ !s->first_field) {
  1719. /* end of image */
  1720. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG2;
  1721. ff_er_frame_end(s);
  1722. MPV_frame_end(s);
  1723. if (s->pict_type == FF_B_TYPE || s->low_delay) {
  1724. *pict= *(AVFrame*)s->current_picture_ptr;
  1725. ff_print_debug_info(s, pict);
  1726. } else {
  1727. s->picture_number++;
  1728. /* latency of 1 frame for I and P frames */
  1729. /* XXX: use another variable than picture_number */
  1730. if (s->last_picture_ptr != NULL) {
  1731. *pict= *(AVFrame*)s->last_picture_ptr;
  1732. ff_print_debug_info(s, pict);
  1733. }
  1734. }
  1735. return 1;
  1736. } else {
  1737. return 0;
  1738. }
  1739. }
  1740. static int mpeg1_decode_sequence(AVCodecContext *avctx,
  1741. const uint8_t *buf, int buf_size)
  1742. {
  1743. Mpeg1Context *s1 = avctx->priv_data;
  1744. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1745. int width,height;
  1746. int i, v, j;
  1747. init_get_bits(&s->gb, buf, buf_size*8);
  1748. width = get_bits(&s->gb, 12);
  1749. height = get_bits(&s->gb, 12);
  1750. if (width <= 0 || height <= 0)
  1751. return -1;
  1752. s->aspect_ratio_info= get_bits(&s->gb, 4);
  1753. if (s->aspect_ratio_info == 0) {
  1754. av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
  1755. if (avctx->error_resilience >= FF_ER_COMPLIANT)
  1756. return -1;
  1757. }
  1758. s->frame_rate_index = get_bits(&s->gb, 4);
  1759. if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
  1760. return -1;
  1761. s->bit_rate = get_bits(&s->gb, 18) * 400;
  1762. if (get_bits1(&s->gb) == 0) /* marker */
  1763. return -1;
  1764. s->width = width;
  1765. s->height = height;
  1766. s->avctx->rc_buffer_size= get_bits(&s->gb, 10) * 1024*16;
  1767. skip_bits(&s->gb, 1);
  1768. /* get matrix */
  1769. if (get_bits1(&s->gb)) {
  1770. for(i=0;i<64;i++) {
  1771. v = get_bits(&s->gb, 8);
  1772. if(v==0){
  1773. av_log(s->avctx, AV_LOG_ERROR, "intra matrix damaged\n");
  1774. return -1;
  1775. }
  1776. j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  1777. s->intra_matrix[j] = v;
  1778. s->chroma_intra_matrix[j] = v;
  1779. }
  1780. #ifdef DEBUG
  1781. dprintf(s->avctx, "intra matrix present\n");
  1782. for(i=0;i<64;i++)
  1783. dprintf(s->avctx, " %d", s->intra_matrix[s->dsp.idct_permutation[i]]);
  1784. dprintf(s->avctx, "\n");
  1785. #endif
  1786. } else {
  1787. for(i=0;i<64;i++) {
  1788. j = s->dsp.idct_permutation[i];
  1789. v = ff_mpeg1_default_intra_matrix[i];
  1790. s->intra_matrix[j] = v;
  1791. s->chroma_intra_matrix[j] = v;
  1792. }
  1793. }
  1794. if (get_bits1(&s->gb)) {
  1795. for(i=0;i<64;i++) {
  1796. v = get_bits(&s->gb, 8);
  1797. if(v==0){
  1798. av_log(s->avctx, AV_LOG_ERROR, "inter matrix damaged\n");
  1799. return -1;
  1800. }
  1801. j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  1802. s->inter_matrix[j] = v;
  1803. s->chroma_inter_matrix[j] = v;
  1804. }
  1805. #ifdef DEBUG
  1806. dprintf(s->avctx, "non intra matrix present\n");
  1807. for(i=0;i<64;i++)
  1808. dprintf(s->avctx, " %d", s->inter_matrix[s->dsp.idct_permutation[i]]);
  1809. dprintf(s->avctx, "\n");
  1810. #endif
  1811. } else {
  1812. for(i=0;i<64;i++) {
  1813. int j= s->dsp.idct_permutation[i];
  1814. v = ff_mpeg1_default_non_intra_matrix[i];
  1815. s->inter_matrix[j] = v;
  1816. s->chroma_inter_matrix[j] = v;
  1817. }
  1818. }
  1819. if(show_bits(&s->gb, 23) != 0){
  1820. av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
  1821. return -1;
  1822. }
  1823. /* we set mpeg2 parameters so that it emulates mpeg1 */
  1824. s->progressive_sequence = 1;
  1825. s->progressive_frame = 1;
  1826. s->picture_structure = PICT_FRAME;
  1827. s->frame_pred_frame_dct = 1;
  1828. s->chroma_format = 1;
  1829. s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG1VIDEO;
  1830. avctx->sub_id = 1; /* indicates mpeg1 */
  1831. s->out_format = FMT_MPEG1;
  1832. s->swap_uv = 0;//AFAIK VCR2 don't have SEQ_HEADER
  1833. if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
  1834. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1835. av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
  1836. s->avctx->rc_buffer_size, s->bit_rate);
  1837. return 0;
  1838. }
  1839. static int vcr2_init_sequence(AVCodecContext *avctx)
  1840. {
  1841. Mpeg1Context *s1 = avctx->priv_data;
  1842. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1843. int i, v;
  1844. /* start new mpeg1 context decoding */
  1845. s->out_format = FMT_MPEG1;
  1846. if (s1->mpeg_enc_ctx_allocated) {
  1847. MPV_common_end(s);
  1848. }
  1849. s->width = avctx->coded_width;
  1850. s->height = avctx->coded_height;
  1851. avctx->has_b_frames= 0; //true?
  1852. s->low_delay= 1;
  1853. if(avctx->xvmc_acceleration){
  1854. avctx->pix_fmt = avctx->get_format(avctx,pixfmt_xvmc_mpg2_420);
  1855. }else{
  1856. avctx->pix_fmt = avctx->get_format(avctx,pixfmt_yuv_420);
  1857. }
  1858. if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT )
  1859. if( avctx->idct_algo == FF_IDCT_AUTO )
  1860. avctx->idct_algo = FF_IDCT_SIMPLE;
  1861. if (MPV_common_init(s) < 0)
  1862. return -1;
  1863. exchange_uv(s);//common init reset pblocks, so we swap them here
  1864. s->swap_uv = 1;// in case of xvmc we need to swap uv for each MB
  1865. s1->mpeg_enc_ctx_allocated = 1;
  1866. for(i=0;i<64;i++) {
  1867. int j= s->dsp.idct_permutation[i];
  1868. v = ff_mpeg1_default_intra_matrix[i];
  1869. s->intra_matrix[j] = v;
  1870. s->chroma_intra_matrix[j] = v;
  1871. v = ff_mpeg1_default_non_intra_matrix[i];
  1872. s->inter_matrix[j] = v;
  1873. s->chroma_inter_matrix[j] = v;
  1874. }
  1875. s->progressive_sequence = 1;
  1876. s->progressive_frame = 1;
  1877. s->picture_structure = PICT_FRAME;
  1878. s->frame_pred_frame_dct = 1;
  1879. s->chroma_format = 1;
  1880. s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
  1881. avctx->sub_id = 2; /* indicates mpeg2 */
  1882. return 0;
  1883. }
  1884. static void mpeg_decode_user_data(AVCodecContext *avctx,
  1885. const uint8_t *buf, int buf_size)
  1886. {
  1887. const uint8_t *p;
  1888. int len, flags;
  1889. p = buf;
  1890. len = buf_size;
  1891. /* we parse the DTG active format information */
  1892. if (len >= 5 &&
  1893. p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
  1894. flags = p[4];
  1895. p += 5;
  1896. len -= 5;
  1897. if (flags & 0x80) {
  1898. /* skip event id */
  1899. if (len < 2)
  1900. return;
  1901. p += 2;
  1902. len -= 2;
  1903. }
  1904. if (flags & 0x40) {
  1905. if (len < 1)
  1906. return;
  1907. avctx->dtg_active_format = p[0] & 0x0f;
  1908. }
  1909. }
  1910. }
  1911. static void mpeg_decode_gop(AVCodecContext *avctx,
  1912. const uint8_t *buf, int buf_size){
  1913. Mpeg1Context *s1 = avctx->priv_data;
  1914. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1915. int drop_frame_flag;
  1916. int time_code_hours, time_code_minutes;
  1917. int time_code_seconds, time_code_pictures;
  1918. int closed_gop, broken_link;
  1919. init_get_bits(&s->gb, buf, buf_size*8);
  1920. drop_frame_flag = get_bits1(&s->gb);
  1921. time_code_hours=get_bits(&s->gb,5);
  1922. time_code_minutes = get_bits(&s->gb,6);
  1923. skip_bits1(&s->gb);//marker bit
  1924. time_code_seconds = get_bits(&s->gb,6);
  1925. time_code_pictures = get_bits(&s->gb,6);
  1926. closed_gop = get_bits1(&s->gb);
  1927. /*broken_link indicate that after editing the
  1928. reference frames of the first B-Frames after GOP I-Frame
  1929. are missing (open gop)*/
  1930. broken_link = get_bits1(&s->gb);
  1931. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1932. av_log(s->avctx, AV_LOG_DEBUG, "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
  1933. time_code_hours, time_code_minutes, time_code_seconds,
  1934. time_code_pictures, closed_gop, broken_link);
  1935. }
  1936. /**
  1937. * finds the end of the current frame in the bitstream.
  1938. * @return the position of the first byte of the next frame, or -1
  1939. */
  1940. int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
  1941. {
  1942. int i;
  1943. uint32_t state= pc->state;
  1944. /* EOF considered as end of frame */
  1945. if (buf_size == 0)
  1946. return 0;
  1947. /*
  1948. 0 frame start -> 1/4
  1949. 1 first_SEQEXT -> 0/2
  1950. 2 first field start -> 3/0
  1951. 3 second_SEQEXT -> 2/0
  1952. 4 searching end
  1953. */
  1954. for(i=0; i<buf_size; i++){
  1955. assert(pc->frame_start_found>=0 && pc->frame_start_found<=4);
  1956. if(pc->frame_start_found&1){
  1957. if(state == EXT_START_CODE && (buf[i]&0xF0) != 0x80)
  1958. pc->frame_start_found--;
  1959. else if(state == EXT_START_CODE+2){
  1960. if((buf[i]&3) == 3) pc->frame_start_found= 0;
  1961. else pc->frame_start_found= (pc->frame_start_found+1)&3;
  1962. }
  1963. state++;
  1964. }else{
  1965. i= ff_find_start_code(buf+i, buf+buf_size, &state) - buf - 1;
  1966. if(pc->frame_start_found==0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE){
  1967. i++;
  1968. pc->frame_start_found=4;
  1969. }
  1970. if(state == SEQ_END_CODE){
  1971. pc->state=-1;
  1972. return i+1;
  1973. }
  1974. if(pc->frame_start_found==2 && state == SEQ_START_CODE)
  1975. pc->frame_start_found= 0;
  1976. if(pc->frame_start_found<4 && state == EXT_START_CODE)
  1977. pc->frame_start_found++;
  1978. if(pc->frame_start_found == 4 && (state&0xFFFFFF00) == 0x100){
  1979. if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
  1980. pc->frame_start_found=0;
  1981. pc->state=-1;
  1982. return i-3;
  1983. }
  1984. }
  1985. }
  1986. }
  1987. pc->state= state;
  1988. return END_NOT_FOUND;
  1989. }
  1990. static int decode_chunks(AVCodecContext *avctx,
  1991. AVFrame *picture, int *data_size,
  1992. const uint8_t *buf, int buf_size);
  1993. /* handle buffering and image synchronisation */
  1994. static int mpeg_decode_frame(AVCodecContext *avctx,
  1995. void *data, int *data_size,
  1996. const uint8_t *buf, int buf_size)
  1997. {
  1998. Mpeg1Context *s = avctx->priv_data;
  1999. AVFrame *picture = data;
  2000. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  2001. dprintf(avctx, "fill_buffer\n");
  2002. if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
  2003. /* special case for last picture */
  2004. if (s2->low_delay==0 && s2->next_picture_ptr) {
  2005. *picture= *(AVFrame*)s2->next_picture_ptr;
  2006. s2->next_picture_ptr= NULL;
  2007. *data_size = sizeof(AVFrame);
  2008. }
  2009. return buf_size;
  2010. }
  2011. if(s2->flags&CODEC_FLAG_TRUNCATED){
  2012. int next= ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size);
  2013. if( ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0 )
  2014. return buf_size;
  2015. }
  2016. #if 0
  2017. if (s->repeat_field % 2 == 1) {
  2018. s->repeat_field++;
  2019. //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
  2020. // s2->picture_number, s->repeat_field);
  2021. if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
  2022. *data_size = sizeof(AVPicture);
  2023. goto the_end;
  2024. }
  2025. }
  2026. #endif
  2027. if(s->mpeg_enc_ctx_allocated==0 && avctx->codec_tag == ff_get_fourcc("VCR2"))
  2028. vcr2_init_sequence(avctx);
  2029. s->slice_count= 0;
  2030. if(avctx->extradata && !avctx->frame_number)
  2031. decode_chunks(avctx, picture, data_size, avctx->extradata, avctx->extradata_size);
  2032. return decode_chunks(avctx, picture, data_size, buf, buf_size);
  2033. }
  2034. static int decode_chunks(AVCodecContext *avctx,
  2035. AVFrame *picture, int *data_size,
  2036. const uint8_t *buf, int buf_size)
  2037. {
  2038. Mpeg1Context *s = avctx->priv_data;
  2039. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  2040. const uint8_t *buf_ptr = buf;
  2041. const uint8_t *buf_end = buf + buf_size;
  2042. int ret, input_size;
  2043. for(;;) {
  2044. /* find start next code */
  2045. uint32_t start_code = -1;
  2046. buf_ptr = ff_find_start_code(buf_ptr,buf_end, &start_code);
  2047. if (start_code > 0x1ff){
  2048. if(s2->pict_type != FF_B_TYPE || avctx->skip_frame <= AVDISCARD_DEFAULT){
  2049. if(avctx->thread_count > 1){
  2050. int i;
  2051. avctx->execute(avctx, slice_decode_thread, (void**)&(s2->thread_context[0]), NULL, s->slice_count);
  2052. for(i=0; i<s->slice_count; i++)
  2053. s2->error_count += s2->thread_context[i]->error_count;
  2054. }
  2055. if (slice_end(avctx, picture)) {
  2056. if(s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
  2057. *data_size = sizeof(AVPicture);
  2058. }
  2059. }
  2060. return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
  2061. }
  2062. input_size = buf_end - buf_ptr;
  2063. if(avctx->debug & FF_DEBUG_STARTCODE){
  2064. av_log(avctx, AV_LOG_DEBUG, "%3X at %td left %d\n", start_code, buf_ptr-buf, input_size);
  2065. }
  2066. /* prepare data for next start code */
  2067. switch(start_code) {
  2068. case SEQ_START_CODE:
  2069. mpeg1_decode_sequence(avctx, buf_ptr,
  2070. input_size);
  2071. break;
  2072. case PICTURE_START_CODE:
  2073. /* we have a complete image : we try to decompress it */
  2074. mpeg1_decode_picture(avctx,
  2075. buf_ptr, input_size);
  2076. break;
  2077. case EXT_START_CODE:
  2078. mpeg_decode_extension(avctx,
  2079. buf_ptr, input_size);
  2080. break;
  2081. case USER_START_CODE:
  2082. mpeg_decode_user_data(avctx,
  2083. buf_ptr, input_size);
  2084. break;
  2085. case GOP_START_CODE:
  2086. s2->first_field=0;
  2087. mpeg_decode_gop(avctx,
  2088. buf_ptr, input_size);
  2089. break;
  2090. default:
  2091. if (start_code >= SLICE_MIN_START_CODE &&
  2092. start_code <= SLICE_MAX_START_CODE) {
  2093. int mb_y= start_code - SLICE_MIN_START_CODE;
  2094. if(s2->last_picture_ptr==NULL){
  2095. /* Skip B-frames if we do not have reference frames. */
  2096. if(s2->pict_type==FF_B_TYPE) break;
  2097. }
  2098. if(s2->next_picture_ptr==NULL){
  2099. /* Skip P-frames if we do not have reference frame no valid header. */
  2100. if(s2->pict_type==FF_P_TYPE && (s2->first_field || s2->picture_structure==PICT_FRAME)) break;
  2101. }
  2102. /* Skip B-frames if we are in a hurry. */
  2103. if(avctx->hurry_up && s2->pict_type==FF_B_TYPE) break;
  2104. if( (avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type==FF_B_TYPE)
  2105. ||(avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type!=FF_I_TYPE)
  2106. || avctx->skip_frame >= AVDISCARD_ALL)
  2107. break;
  2108. /* Skip everything if we are in a hurry>=5. */
  2109. if(avctx->hurry_up>=5) break;
  2110. if (!s->mpeg_enc_ctx_allocated) break;
  2111. if(s2->codec_id == CODEC_ID_MPEG2VIDEO){
  2112. if(mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
  2113. break;
  2114. }
  2115. if(s2->first_slice){
  2116. s2->first_slice=0;
  2117. if(mpeg_field_start(s2) < 0)
  2118. return -1;
  2119. }
  2120. if(!s2->current_picture_ptr){
  2121. av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\n");
  2122. return -1;
  2123. }
  2124. if(avctx->thread_count > 1){
  2125. int threshold= (s2->mb_height*s->slice_count + avctx->thread_count/2) / avctx->thread_count;
  2126. if(threshold <= mb_y){
  2127. MpegEncContext *thread_context= s2->thread_context[s->slice_count];
  2128. thread_context->start_mb_y= mb_y;
  2129. thread_context->end_mb_y = s2->mb_height;
  2130. if(s->slice_count){
  2131. s2->thread_context[s->slice_count-1]->end_mb_y= mb_y;
  2132. ff_update_duplicate_context(thread_context, s2);
  2133. }
  2134. init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
  2135. s->slice_count++;
  2136. }
  2137. buf_ptr += 2; //FIXME add minimum num of bytes per slice
  2138. }else{
  2139. ret = mpeg_decode_slice(s, mb_y, &buf_ptr, input_size);
  2140. emms_c();
  2141. if(ret < 0){
  2142. if(s2->resync_mb_x>=0 && s2->resync_mb_y>=0)
  2143. 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);
  2144. }else{
  2145. 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);
  2146. }
  2147. }
  2148. }
  2149. break;
  2150. }
  2151. }
  2152. }
  2153. static int mpeg_decode_end(AVCodecContext *avctx)
  2154. {
  2155. Mpeg1Context *s = avctx->priv_data;
  2156. if (s->mpeg_enc_ctx_allocated)
  2157. MPV_common_end(&s->mpeg_enc_ctx);
  2158. return 0;
  2159. }
  2160. AVCodec mpeg1video_decoder = {
  2161. "mpeg1video",
  2162. CODEC_TYPE_VIDEO,
  2163. CODEC_ID_MPEG1VIDEO,
  2164. sizeof(Mpeg1Context),
  2165. mpeg_decode_init,
  2166. NULL,
  2167. mpeg_decode_end,
  2168. mpeg_decode_frame,
  2169. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  2170. .flush= ff_mpeg_flush,
  2171. .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
  2172. };
  2173. AVCodec mpeg2video_decoder = {
  2174. "mpeg2video",
  2175. CODEC_TYPE_VIDEO,
  2176. CODEC_ID_MPEG2VIDEO,
  2177. sizeof(Mpeg1Context),
  2178. mpeg_decode_init,
  2179. NULL,
  2180. mpeg_decode_end,
  2181. mpeg_decode_frame,
  2182. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  2183. .flush= ff_mpeg_flush,
  2184. .long_name= NULL_IF_CONFIG_SMALL("MPEG-2 video"),
  2185. };
  2186. //legacy decoder
  2187. AVCodec mpegvideo_decoder = {
  2188. "mpegvideo",
  2189. CODEC_TYPE_VIDEO,
  2190. CODEC_ID_MPEG2VIDEO,
  2191. sizeof(Mpeg1Context),
  2192. mpeg_decode_init,
  2193. NULL,
  2194. mpeg_decode_end,
  2195. mpeg_decode_frame,
  2196. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  2197. .flush= ff_mpeg_flush,
  2198. .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
  2199. };
  2200. #ifdef HAVE_XVMC
  2201. static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx){
  2202. Mpeg1Context *s;
  2203. if( avctx->thread_count > 1)
  2204. return -1;
  2205. if( !(avctx->slice_flags & SLICE_FLAG_CODED_ORDER) )
  2206. return -1;
  2207. if( !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD) ){
  2208. dprintf(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
  2209. }
  2210. mpeg_decode_init(avctx);
  2211. s = avctx->priv_data;
  2212. avctx->pix_fmt = PIX_FMT_XVMC_MPEG2_IDCT;
  2213. avctx->xvmc_acceleration = 2;//2 - the blocks are packed!
  2214. return 0;
  2215. }
  2216. AVCodec mpeg_xvmc_decoder = {
  2217. "mpegvideo_xvmc",
  2218. CODEC_TYPE_VIDEO,
  2219. CODEC_ID_MPEG2VIDEO_XVMC,
  2220. sizeof(Mpeg1Context),
  2221. mpeg_mc_decode_init,
  2222. NULL,
  2223. mpeg_decode_end,
  2224. mpeg_decode_frame,
  2225. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
  2226. .flush= ff_mpeg_flush,
  2227. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video XvMC (X-Video Motion Compensation)"),
  2228. };
  2229. #endif
  2230. /* this is ugly i know, but the alternative is too make
  2231. hundreds of vars global and prefix them with ff_mpeg1_
  2232. which is far uglier. */
  2233. #include "mdec.c"