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.

2558 lines
86KB

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