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.

2489 lines
84KB

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