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.

2510 lines
85KB

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