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.

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