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.

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