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.

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