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
84KB

  1. /*
  2. * MPEG-1/2 decoder
  3. * Copyright (c) 2000,2001 Fabrice Bellard.
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file mpeg12.c
  24. * MPEG-1/2 decoder
  25. */
  26. //#define DEBUG
  27. #include "avcodec.h"
  28. #include "dsputil.h"
  29. #include "mpegvideo.h"
  30. #include "mpeg12.h"
  31. #include "mpeg12data.h"
  32. #include "mpeg12decdata.h"
  33. #include "bytestream.h"
  34. //#undef NDEBUG
  35. //#include <assert.h>
  36. #define MV_VLC_BITS 9
  37. #define MBINCR_VLC_BITS 9
  38. #define MB_PAT_VLC_BITS 9
  39. #define MB_PTYPE_VLC_BITS 6
  40. #define MB_BTYPE_VLC_BITS 6
  41. static inline int mpeg1_decode_block_inter(MpegEncContext *s,
  42. DCTELEM *block,
  43. int n);
  44. static inline int mpeg1_decode_block_intra(MpegEncContext *s,
  45. DCTELEM *block,
  46. int n);
  47. static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n);
  48. static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
  49. DCTELEM *block,
  50. int n);
  51. static inline int mpeg2_decode_block_intra(MpegEncContext *s,
  52. DCTELEM *block,
  53. int n);
  54. static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s, DCTELEM *block, int n);
  55. static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n);
  56. static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred);
  57. static void exchange_uv(MpegEncContext *s);
  58. 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. static enum PixelFormat mpeg_get_pixelformat(AVCodecContext *avctx){
  1094. Mpeg1Context *s1 = avctx->priv_data;
  1095. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1096. if(avctx->xvmc_acceleration)
  1097. return avctx->get_format(avctx,pixfmt_xvmc_mpg2_420);
  1098. else{
  1099. if(s->chroma_format < 2)
  1100. return PIX_FMT_YUV420P;
  1101. else if(s->chroma_format == 2)
  1102. return PIX_FMT_YUV422P;
  1103. else
  1104. return PIX_FMT_YUV444P;
  1105. }
  1106. }
  1107. /* Call this function when we know all parameters.
  1108. * It may be called in different places for MPEG-1 and MPEG-2. */
  1109. static int mpeg_decode_postinit(AVCodecContext *avctx){
  1110. Mpeg1Context *s1 = avctx->priv_data;
  1111. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1112. uint8_t old_permutation[64];
  1113. if (
  1114. (s1->mpeg_enc_ctx_allocated == 0)||
  1115. avctx->coded_width != s->width ||
  1116. avctx->coded_height != s->height||
  1117. s1->save_width != s->width ||
  1118. s1->save_height != s->height ||
  1119. s1->save_aspect_info != s->aspect_ratio_info||
  1120. 0)
  1121. {
  1122. if (s1->mpeg_enc_ctx_allocated) {
  1123. ParseContext pc= s->parse_context;
  1124. s->parse_context.buffer=0;
  1125. MPV_common_end(s);
  1126. s->parse_context= pc;
  1127. }
  1128. if( (s->width == 0 )||(s->height == 0))
  1129. return -2;
  1130. avcodec_set_dimensions(avctx, s->width, s->height);
  1131. avctx->bit_rate = s->bit_rate;
  1132. s1->save_aspect_info = s->aspect_ratio_info;
  1133. s1->save_width = s->width;
  1134. s1->save_height = s->height;
  1135. /* low_delay may be forced, in this case we will have B-frames
  1136. * that behave like P-frames. */
  1137. avctx->has_b_frames = !(s->low_delay);
  1138. if(avctx->sub_id==1){//s->codec_id==avctx->codec_id==CODEC_ID
  1139. //MPEG-1 fps
  1140. avctx->time_base.den= ff_frame_rate_tab[s->frame_rate_index].num;
  1141. avctx->time_base.num= ff_frame_rate_tab[s->frame_rate_index].den;
  1142. //MPEG-1 aspect
  1143. avctx->sample_aspect_ratio= av_d2q(
  1144. 1.0/ff_mpeg1_aspect[s->aspect_ratio_info], 255);
  1145. }else{//MPEG-2
  1146. //MPEG-2 fps
  1147. av_reduce(
  1148. &s->avctx->time_base.den,
  1149. &s->avctx->time_base.num,
  1150. ff_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num,
  1151. ff_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
  1152. 1<<30);
  1153. //MPEG-2 aspect
  1154. if(s->aspect_ratio_info > 1){
  1155. //we ignore the spec here as reality does not match the spec, see for example
  1156. // res_change_ffmpeg_aspect.ts and sequence-display-aspect.mpg
  1157. if( (s1->pan_scan.width == 0 )||(s1->pan_scan.height == 0) || 1){
  1158. s->avctx->sample_aspect_ratio=
  1159. av_div_q(
  1160. ff_mpeg2_aspect[s->aspect_ratio_info],
  1161. (AVRational){s->width, s->height}
  1162. );
  1163. }else{
  1164. s->avctx->sample_aspect_ratio=
  1165. av_div_q(
  1166. ff_mpeg2_aspect[s->aspect_ratio_info],
  1167. (AVRational){s1->pan_scan.width, s1->pan_scan.height}
  1168. );
  1169. }
  1170. }else{
  1171. s->avctx->sample_aspect_ratio=
  1172. ff_mpeg2_aspect[s->aspect_ratio_info];
  1173. }
  1174. }//MPEG-2
  1175. avctx->pix_fmt = mpeg_get_pixelformat(avctx);
  1176. //until then pix_fmt may be changed right after codec init
  1177. if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT )
  1178. if( avctx->idct_algo == FF_IDCT_AUTO )
  1179. avctx->idct_algo = FF_IDCT_SIMPLE;
  1180. /* Quantization matrices may need reordering
  1181. * if DCT permutation is changed. */
  1182. memcpy(old_permutation,s->dsp.idct_permutation,64*sizeof(uint8_t));
  1183. if (MPV_common_init(s) < 0)
  1184. return -2;
  1185. quant_matrix_rebuild(s->intra_matrix, old_permutation,s->dsp.idct_permutation);
  1186. quant_matrix_rebuild(s->inter_matrix, old_permutation,s->dsp.idct_permutation);
  1187. quant_matrix_rebuild(s->chroma_intra_matrix,old_permutation,s->dsp.idct_permutation);
  1188. quant_matrix_rebuild(s->chroma_inter_matrix,old_permutation,s->dsp.idct_permutation);
  1189. s1->mpeg_enc_ctx_allocated = 1;
  1190. }
  1191. return 0;
  1192. }
  1193. static int mpeg1_decode_picture(AVCodecContext *avctx,
  1194. const uint8_t *buf, int buf_size)
  1195. {
  1196. Mpeg1Context *s1 = avctx->priv_data;
  1197. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1198. int ref, f_code, vbv_delay;
  1199. if(mpeg_decode_postinit(s->avctx) < 0)
  1200. return -2;
  1201. init_get_bits(&s->gb, buf, buf_size*8);
  1202. ref = get_bits(&s->gb, 10); /* temporal ref */
  1203. s->pict_type = get_bits(&s->gb, 3);
  1204. if(s->pict_type == 0 || s->pict_type > 3)
  1205. return -1;
  1206. vbv_delay= get_bits(&s->gb, 16);
  1207. if (s->pict_type == FF_P_TYPE || s->pict_type == FF_B_TYPE) {
  1208. s->full_pel[0] = get_bits1(&s->gb);
  1209. f_code = get_bits(&s->gb, 3);
  1210. if (f_code == 0 && avctx->error_recognition >= FF_ER_COMPLIANT)
  1211. return -1;
  1212. s->mpeg_f_code[0][0] = f_code;
  1213. s->mpeg_f_code[0][1] = f_code;
  1214. }
  1215. if (s->pict_type == FF_B_TYPE) {
  1216. s->full_pel[1] = get_bits1(&s->gb);
  1217. f_code = get_bits(&s->gb, 3);
  1218. if (f_code == 0 && avctx->error_recognition >= FF_ER_COMPLIANT)
  1219. return -1;
  1220. s->mpeg_f_code[1][0] = f_code;
  1221. s->mpeg_f_code[1][1] = f_code;
  1222. }
  1223. s->current_picture.pict_type= s->pict_type;
  1224. s->current_picture.key_frame= s->pict_type == FF_I_TYPE;
  1225. if(avctx->debug & FF_DEBUG_PICT_INFO)
  1226. av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
  1227. s->y_dc_scale = 8;
  1228. s->c_dc_scale = 8;
  1229. s->first_slice = 1;
  1230. return 0;
  1231. }
  1232. static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
  1233. {
  1234. MpegEncContext *s= &s1->mpeg_enc_ctx;
  1235. int horiz_size_ext, vert_size_ext;
  1236. int bit_rate_ext;
  1237. skip_bits(&s->gb, 1); /* profile and level esc*/
  1238. s->avctx->profile= get_bits(&s->gb, 3);
  1239. s->avctx->level= get_bits(&s->gb, 4);
  1240. s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
  1241. s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
  1242. horiz_size_ext = get_bits(&s->gb, 2);
  1243. vert_size_ext = get_bits(&s->gb, 2);
  1244. s->width |= (horiz_size_ext << 12);
  1245. s->height |= (vert_size_ext << 12);
  1246. bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
  1247. s->bit_rate += (bit_rate_ext << 18) * 400;
  1248. skip_bits1(&s->gb); /* marker */
  1249. s->avctx->rc_buffer_size += get_bits(&s->gb, 8)*1024*16<<10;
  1250. s->low_delay = get_bits1(&s->gb);
  1251. if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
  1252. s1->frame_rate_ext.num = get_bits(&s->gb, 2)+1;
  1253. s1->frame_rate_ext.den = get_bits(&s->gb, 5)+1;
  1254. dprintf(s->avctx, "sequence extension\n");
  1255. s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
  1256. s->avctx->sub_id = 2; /* indicates MPEG-2 found */
  1257. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1258. av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
  1259. s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
  1260. }
  1261. static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
  1262. {
  1263. MpegEncContext *s= &s1->mpeg_enc_ctx;
  1264. int color_description, w, h;
  1265. skip_bits(&s->gb, 3); /* video format */
  1266. color_description= get_bits1(&s->gb);
  1267. if(color_description){
  1268. skip_bits(&s->gb, 8); /* color primaries */
  1269. skip_bits(&s->gb, 8); /* transfer_characteristics */
  1270. skip_bits(&s->gb, 8); /* matrix_coefficients */
  1271. }
  1272. w= get_bits(&s->gb, 14);
  1273. skip_bits(&s->gb, 1); //marker
  1274. h= get_bits(&s->gb, 14);
  1275. skip_bits(&s->gb, 1); //marker
  1276. s1->pan_scan.width= 16*w;
  1277. s1->pan_scan.height=16*h;
  1278. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1279. av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
  1280. }
  1281. static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
  1282. {
  1283. MpegEncContext *s= &s1->mpeg_enc_ctx;
  1284. int i,nofco;
  1285. nofco = 1;
  1286. if(s->progressive_sequence){
  1287. if(s->repeat_first_field){
  1288. nofco++;
  1289. if(s->top_field_first)
  1290. nofco++;
  1291. }
  1292. }else{
  1293. if(s->picture_structure == PICT_FRAME){
  1294. nofco++;
  1295. if(s->repeat_first_field)
  1296. nofco++;
  1297. }
  1298. }
  1299. for(i=0; i<nofco; i++){
  1300. s1->pan_scan.position[i][0]= get_sbits(&s->gb, 16);
  1301. skip_bits(&s->gb, 1); //marker
  1302. s1->pan_scan.position[i][1]= get_sbits(&s->gb, 16);
  1303. skip_bits(&s->gb, 1); //marker
  1304. }
  1305. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1306. av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
  1307. s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
  1308. s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
  1309. s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]
  1310. );
  1311. }
  1312. static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
  1313. {
  1314. int i, v, j;
  1315. dprintf(s->avctx, "matrix extension\n");
  1316. if (get_bits1(&s->gb)) {
  1317. for(i=0;i<64;i++) {
  1318. v = get_bits(&s->gb, 8);
  1319. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  1320. s->intra_matrix[j] = v;
  1321. s->chroma_intra_matrix[j] = v;
  1322. }
  1323. }
  1324. if (get_bits1(&s->gb)) {
  1325. for(i=0;i<64;i++) {
  1326. v = get_bits(&s->gb, 8);
  1327. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  1328. s->inter_matrix[j] = v;
  1329. s->chroma_inter_matrix[j] = v;
  1330. }
  1331. }
  1332. if (get_bits1(&s->gb)) {
  1333. for(i=0;i<64;i++) {
  1334. v = get_bits(&s->gb, 8);
  1335. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  1336. s->chroma_intra_matrix[j] = v;
  1337. }
  1338. }
  1339. if (get_bits1(&s->gb)) {
  1340. for(i=0;i<64;i++) {
  1341. v = get_bits(&s->gb, 8);
  1342. j= s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  1343. s->chroma_inter_matrix[j] = v;
  1344. }
  1345. }
  1346. }
  1347. static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
  1348. {
  1349. MpegEncContext *s= &s1->mpeg_enc_ctx;
  1350. s->full_pel[0] = s->full_pel[1] = 0;
  1351. s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
  1352. s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
  1353. s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
  1354. s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
  1355. if(!s->pict_type && s1->mpeg_enc_ctx_allocated){
  1356. av_log(s->avctx, AV_LOG_ERROR, "Missing picture start code, guessing missing values\n");
  1357. if(s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1]==15){
  1358. if(s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
  1359. s->pict_type= FF_I_TYPE;
  1360. else
  1361. s->pict_type= FF_P_TYPE;
  1362. }else
  1363. s->pict_type= FF_B_TYPE;
  1364. s->current_picture.pict_type= s->pict_type;
  1365. s->current_picture.key_frame= s->pict_type == FF_I_TYPE;
  1366. s->first_slice= 1;
  1367. }
  1368. s->intra_dc_precision = get_bits(&s->gb, 2);
  1369. s->picture_structure = get_bits(&s->gb, 2);
  1370. s->top_field_first = get_bits1(&s->gb);
  1371. s->frame_pred_frame_dct = get_bits1(&s->gb);
  1372. s->concealment_motion_vectors = get_bits1(&s->gb);
  1373. s->q_scale_type = get_bits1(&s->gb);
  1374. s->intra_vlc_format = get_bits1(&s->gb);
  1375. s->alternate_scan = get_bits1(&s->gb);
  1376. s->repeat_first_field = get_bits1(&s->gb);
  1377. s->chroma_420_type = get_bits1(&s->gb);
  1378. s->progressive_frame = get_bits1(&s->gb);
  1379. if(s->picture_structure == PICT_FRAME){
  1380. s->first_field=0;
  1381. s->v_edge_pos= 16*s->mb_height;
  1382. }else{
  1383. s->first_field ^= 1;
  1384. s->v_edge_pos= 8*s->mb_height;
  1385. memset(s->mbskip_table, 0, s->mb_stride*s->mb_height);
  1386. }
  1387. if(s->alternate_scan){
  1388. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
  1389. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
  1390. }else{
  1391. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
  1392. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
  1393. }
  1394. /* composite display not parsed */
  1395. dprintf(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
  1396. dprintf(s->avctx, "picture_structure=%d\n", s->picture_structure);
  1397. dprintf(s->avctx, "top field first=%d\n", s->top_field_first);
  1398. dprintf(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
  1399. dprintf(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
  1400. dprintf(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
  1401. dprintf(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
  1402. dprintf(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
  1403. dprintf(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
  1404. }
  1405. static void mpeg_decode_extension(AVCodecContext *avctx,
  1406. const uint8_t *buf, int buf_size)
  1407. {
  1408. Mpeg1Context *s1 = avctx->priv_data;
  1409. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1410. int ext_type;
  1411. init_get_bits(&s->gb, buf, buf_size*8);
  1412. ext_type = get_bits(&s->gb, 4);
  1413. switch(ext_type) {
  1414. case 0x1:
  1415. mpeg_decode_sequence_extension(s1);
  1416. break;
  1417. case 0x2:
  1418. mpeg_decode_sequence_display_extension(s1);
  1419. break;
  1420. case 0x3:
  1421. mpeg_decode_quant_matrix_extension(s);
  1422. break;
  1423. case 0x7:
  1424. mpeg_decode_picture_display_extension(s1);
  1425. break;
  1426. case 0x8:
  1427. mpeg_decode_picture_coding_extension(s1);
  1428. break;
  1429. }
  1430. }
  1431. static void exchange_uv(MpegEncContext *s){
  1432. short * tmp = s->pblocks[4];
  1433. s->pblocks[4] = s->pblocks[5];
  1434. s->pblocks[5] = tmp;
  1435. }
  1436. static int mpeg_field_start(MpegEncContext *s){
  1437. AVCodecContext *avctx= s->avctx;
  1438. Mpeg1Context *s1 = (Mpeg1Context*)s;
  1439. /* start frame decoding */
  1440. if(s->first_field || s->picture_structure==PICT_FRAME){
  1441. if(MPV_frame_start(s, avctx) < 0)
  1442. return -1;
  1443. ff_er_frame_start(s);
  1444. /* first check if we must repeat the frame */
  1445. s->current_picture_ptr->repeat_pict = 0;
  1446. if (s->repeat_first_field) {
  1447. if (s->progressive_sequence) {
  1448. if (s->top_field_first)
  1449. s->current_picture_ptr->repeat_pict = 4;
  1450. else
  1451. s->current_picture_ptr->repeat_pict = 2;
  1452. } else if (s->progressive_frame) {
  1453. s->current_picture_ptr->repeat_pict = 1;
  1454. }
  1455. }
  1456. *s->current_picture_ptr->pan_scan= s1->pan_scan;
  1457. }else{ //second field
  1458. int i;
  1459. if(!s->current_picture_ptr){
  1460. av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
  1461. return -1;
  1462. }
  1463. for(i=0; i<4; i++){
  1464. s->current_picture.data[i] = s->current_picture_ptr->data[i];
  1465. if(s->picture_structure == PICT_BOTTOM_FIELD){
  1466. s->current_picture.data[i] += s->current_picture_ptr->linesize[i];
  1467. }
  1468. }
  1469. }
  1470. #ifdef CONFIG_XVMC
  1471. // MPV_frame_start will call this function too,
  1472. // but we need to call it on every field
  1473. if(s->avctx->xvmc_acceleration)
  1474. XVMC_field_start(s,avctx);
  1475. #endif
  1476. return 0;
  1477. }
  1478. #define DECODE_SLICE_ERROR -1
  1479. #define DECODE_SLICE_OK 0
  1480. /**
  1481. * decodes a slice. MpegEncContext.mb_y must be set to the MB row from the startcode
  1482. * @return DECODE_SLICE_ERROR if the slice is damaged<br>
  1483. * DECODE_SLICE_OK if this slice is ok<br>
  1484. */
  1485. static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y,
  1486. const uint8_t **buf, int buf_size)
  1487. {
  1488. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1489. AVCodecContext *avctx= s->avctx;
  1490. const int field_pic= s->picture_structure != PICT_FRAME;
  1491. const int lowres= s->avctx->lowres;
  1492. s->resync_mb_x=
  1493. s->resync_mb_y= -1;
  1494. if (mb_y<<field_pic >= s->mb_height){
  1495. av_log(s->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s->mb_height);
  1496. return -1;
  1497. }
  1498. init_get_bits(&s->gb, *buf, buf_size*8);
  1499. ff_mpeg1_clean_buffers(s);
  1500. s->interlaced_dct = 0;
  1501. s->qscale = get_qscale(s);
  1502. if(s->qscale == 0){
  1503. av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
  1504. return -1;
  1505. }
  1506. /* extra slice info */
  1507. while (get_bits1(&s->gb) != 0) {
  1508. skip_bits(&s->gb, 8);
  1509. }
  1510. s->mb_x=0;
  1511. for(;;) {
  1512. int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  1513. if (code < 0){
  1514. av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
  1515. return -1;
  1516. }
  1517. if (code >= 33) {
  1518. if (code == 33) {
  1519. s->mb_x += 33;
  1520. }
  1521. /* otherwise, stuffing, nothing to do */
  1522. } else {
  1523. s->mb_x += code;
  1524. break;
  1525. }
  1526. }
  1527. if(s->mb_x >= (unsigned)s->mb_width){
  1528. av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
  1529. return -1;
  1530. }
  1531. s->resync_mb_x= s->mb_x;
  1532. s->resync_mb_y= s->mb_y= mb_y;
  1533. s->mb_skip_run= 0;
  1534. ff_init_block_index(s);
  1535. if (s->mb_y==0 && s->mb_x==0 && (s->first_field || s->picture_structure==PICT_FRAME)) {
  1536. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  1537. 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",
  1538. 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],
  1539. s->pict_type == FF_I_TYPE ? "I" : (s->pict_type == FF_P_TYPE ? "P" : (s->pict_type == FF_B_TYPE ? "B" : "S")),
  1540. s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
  1541. s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
  1542. s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
  1543. }
  1544. }
  1545. for(;;) {
  1546. #ifdef CONFIG_XVMC
  1547. //If 1, we memcpy blocks in xvmcvideo.
  1548. if(s->avctx->xvmc_acceleration > 1)
  1549. XVMC_init_block(s);//set s->block
  1550. #endif
  1551. if(mpeg_decode_mb(s, s->block) < 0)
  1552. return -1;
  1553. if(s->current_picture.motion_val[0] && !s->encoding){ //note motion_val is normally NULL unless we want to extract the MVs
  1554. const int wrap = field_pic ? 2*s->b8_stride : s->b8_stride;
  1555. int xy = s->mb_x*2 + s->mb_y*2*wrap;
  1556. int motion_x, motion_y, dir, i;
  1557. if(field_pic && !s->first_field)
  1558. xy += wrap/2;
  1559. for(i=0; i<2; i++){
  1560. for(dir=0; dir<2; dir++){
  1561. if (s->mb_intra || (dir==1 && s->pict_type != FF_B_TYPE)) {
  1562. motion_x = motion_y = 0;
  1563. }else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)){
  1564. motion_x = s->mv[dir][0][0];
  1565. motion_y = s->mv[dir][0][1];
  1566. } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
  1567. motion_x = s->mv[dir][i][0];
  1568. motion_y = s->mv[dir][i][1];
  1569. }
  1570. s->current_picture.motion_val[dir][xy ][0] = motion_x;
  1571. s->current_picture.motion_val[dir][xy ][1] = motion_y;
  1572. s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
  1573. s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
  1574. s->current_picture.ref_index [dir][xy ]=
  1575. s->current_picture.ref_index [dir][xy + 1]= s->field_select[dir][i];
  1576. assert(s->field_select[dir][i]==0 || s->field_select[dir][i]==1);
  1577. }
  1578. xy += wrap;
  1579. }
  1580. }
  1581. s->dest[0] += 16 >> lowres;
  1582. s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
  1583. s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
  1584. MPV_decode_mb(s, s->block);
  1585. if (++s->mb_x >= s->mb_width) {
  1586. const int mb_size= 16>>s->avctx->lowres;
  1587. ff_draw_horiz_band(s, mb_size*s->mb_y, mb_size);
  1588. s->mb_x = 0;
  1589. s->mb_y++;
  1590. if(s->mb_y<<field_pic >= s->mb_height){
  1591. int left= s->gb.size_in_bits - get_bits_count(&s->gb);
  1592. int is_d10= s->chroma_format==2 && s->pict_type==FF_I_TYPE && avctx->profile==0 && avctx->level==5
  1593. && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
  1594. && s->progressive_frame == 0 /* vbv_delay == 0xBBB || 0xE10*/;
  1595. if(left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
  1596. || (avctx->error_recognition >= FF_ER_AGGRESSIVE && left>8)){
  1597. av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
  1598. return -1;
  1599. }else
  1600. goto eos;
  1601. }
  1602. ff_init_block_index(s);
  1603. }
  1604. /* skip mb handling */
  1605. if (s->mb_skip_run == -1) {
  1606. /* read increment again */
  1607. s->mb_skip_run = 0;
  1608. for(;;) {
  1609. int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  1610. if (code < 0){
  1611. av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
  1612. return -1;
  1613. }
  1614. if (code >= 33) {
  1615. if (code == 33) {
  1616. s->mb_skip_run += 33;
  1617. }else if(code == 35){
  1618. if(s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0){
  1619. av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
  1620. return -1;
  1621. }
  1622. goto eos; /* end of slice */
  1623. }
  1624. /* otherwise, stuffing, nothing to do */
  1625. } else {
  1626. s->mb_skip_run += code;
  1627. break;
  1628. }
  1629. }
  1630. if(s->mb_skip_run){
  1631. int i;
  1632. if(s->pict_type == FF_I_TYPE){
  1633. av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
  1634. return -1;
  1635. }
  1636. /* skip mb */
  1637. s->mb_intra = 0;
  1638. for(i=0;i<12;i++)
  1639. s->block_last_index[i] = -1;
  1640. if(s->picture_structure == PICT_FRAME)
  1641. s->mv_type = MV_TYPE_16X16;
  1642. else
  1643. s->mv_type = MV_TYPE_FIELD;
  1644. if (s->pict_type == FF_P_TYPE) {
  1645. /* if P type, zero motion vector is implied */
  1646. s->mv_dir = MV_DIR_FORWARD;
  1647. s->mv[0][0][0] = s->mv[0][0][1] = 0;
  1648. s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
  1649. s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
  1650. s->field_select[0][0]= s->picture_structure - 1;
  1651. } else {
  1652. /* if B type, reuse previous vectors and directions */
  1653. s->mv[0][0][0] = s->last_mv[0][0][0];
  1654. s->mv[0][0][1] = s->last_mv[0][0][1];
  1655. s->mv[1][0][0] = s->last_mv[1][0][0];
  1656. s->mv[1][0][1] = s->last_mv[1][0][1];
  1657. }
  1658. }
  1659. }
  1660. }
  1661. eos: // end of slice
  1662. *buf += (get_bits_count(&s->gb)-1)/8;
  1663. //printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
  1664. return 0;
  1665. }
  1666. static int slice_decode_thread(AVCodecContext *c, void *arg){
  1667. MpegEncContext *s= *(void**)arg;
  1668. const uint8_t *buf= s->gb.buffer;
  1669. int mb_y= s->start_mb_y;
  1670. s->error_count= 3*(s->end_mb_y - s->start_mb_y)*s->mb_width;
  1671. for(;;){
  1672. uint32_t start_code;
  1673. int ret;
  1674. ret= mpeg_decode_slice((Mpeg1Context*)s, mb_y, &buf, s->gb.buffer_end - buf);
  1675. emms_c();
  1676. //av_log(c, AV_LOG_DEBUG, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
  1677. //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);
  1678. if(ret < 0){
  1679. if(s->resync_mb_x>=0 && s->resync_mb_y>=0)
  1680. 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);
  1681. }else{
  1682. 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);
  1683. }
  1684. if(s->mb_y == s->end_mb_y)
  1685. return 0;
  1686. start_code= -1;
  1687. buf = ff_find_start_code(buf, s->gb.buffer_end, &start_code);
  1688. mb_y= start_code - SLICE_MIN_START_CODE;
  1689. if(mb_y < 0 || mb_y >= s->end_mb_y)
  1690. return -1;
  1691. }
  1692. return 0; //not reached
  1693. }
  1694. /**
  1695. * Handles slice ends.
  1696. * @return 1 if it seems to be the last slice
  1697. */
  1698. static int slice_end(AVCodecContext *avctx, AVFrame *pict)
  1699. {
  1700. Mpeg1Context *s1 = avctx->priv_data;
  1701. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1702. if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
  1703. return 0;
  1704. #ifdef CONFIG_XVMC
  1705. if(s->avctx->xvmc_acceleration)
  1706. XVMC_field_end(s);
  1707. #endif
  1708. /* end of slice reached */
  1709. if (/*s->mb_y<<field_pic == s->mb_height &&*/ !s->first_field) {
  1710. /* end of image */
  1711. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG2;
  1712. ff_er_frame_end(s);
  1713. MPV_frame_end(s);
  1714. if (s->pict_type == FF_B_TYPE || s->low_delay) {
  1715. *pict= *(AVFrame*)s->current_picture_ptr;
  1716. ff_print_debug_info(s, pict);
  1717. } else {
  1718. s->picture_number++;
  1719. /* latency of 1 frame for I- and P-frames */
  1720. /* XXX: use another variable than picture_number */
  1721. if (s->last_picture_ptr != NULL) {
  1722. *pict= *(AVFrame*)s->last_picture_ptr;
  1723. ff_print_debug_info(s, pict);
  1724. }
  1725. }
  1726. return 1;
  1727. } else {
  1728. return 0;
  1729. }
  1730. }
  1731. static int mpeg1_decode_sequence(AVCodecContext *avctx,
  1732. const uint8_t *buf, int buf_size)
  1733. {
  1734. Mpeg1Context *s1 = avctx->priv_data;
  1735. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1736. int width,height;
  1737. int i, v, j;
  1738. init_get_bits(&s->gb, buf, buf_size*8);
  1739. width = get_bits(&s->gb, 12);
  1740. height = get_bits(&s->gb, 12);
  1741. if (width <= 0 || height <= 0)
  1742. return -1;
  1743. s->aspect_ratio_info= get_bits(&s->gb, 4);
  1744. if (s->aspect_ratio_info == 0) {
  1745. av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
  1746. if (avctx->error_recognition >= FF_ER_COMPLIANT)
  1747. return -1;
  1748. }
  1749. s->frame_rate_index = get_bits(&s->gb, 4);
  1750. if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
  1751. return -1;
  1752. s->bit_rate = get_bits(&s->gb, 18) * 400;
  1753. if (get_bits1(&s->gb) == 0) /* marker */
  1754. return -1;
  1755. s->width = width;
  1756. s->height = height;
  1757. s->avctx->rc_buffer_size= get_bits(&s->gb, 10) * 1024*16;
  1758. skip_bits(&s->gb, 1);
  1759. /* get matrix */
  1760. if (get_bits1(&s->gb)) {
  1761. for(i=0;i<64;i++) {
  1762. v = get_bits(&s->gb, 8);
  1763. if(v==0){
  1764. av_log(s->avctx, AV_LOG_ERROR, "intra matrix damaged\n");
  1765. return -1;
  1766. }
  1767. j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  1768. s->intra_matrix[j] = v;
  1769. s->chroma_intra_matrix[j] = v;
  1770. }
  1771. #ifdef DEBUG
  1772. dprintf(s->avctx, "intra matrix present\n");
  1773. for(i=0;i<64;i++)
  1774. dprintf(s->avctx, " %d", s->intra_matrix[s->dsp.idct_permutation[i]]);
  1775. dprintf(s->avctx, "\n");
  1776. #endif
  1777. } else {
  1778. for(i=0;i<64;i++) {
  1779. j = s->dsp.idct_permutation[i];
  1780. v = ff_mpeg1_default_intra_matrix[i];
  1781. s->intra_matrix[j] = v;
  1782. s->chroma_intra_matrix[j] = v;
  1783. }
  1784. }
  1785. if (get_bits1(&s->gb)) {
  1786. for(i=0;i<64;i++) {
  1787. v = get_bits(&s->gb, 8);
  1788. if(v==0){
  1789. av_log(s->avctx, AV_LOG_ERROR, "inter matrix damaged\n");
  1790. return -1;
  1791. }
  1792. j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  1793. s->inter_matrix[j] = v;
  1794. s->chroma_inter_matrix[j] = v;
  1795. }
  1796. #ifdef DEBUG
  1797. dprintf(s->avctx, "non-intra matrix present\n");
  1798. for(i=0;i<64;i++)
  1799. dprintf(s->avctx, " %d", s->inter_matrix[s->dsp.idct_permutation[i]]);
  1800. dprintf(s->avctx, "\n");
  1801. #endif
  1802. } else {
  1803. for(i=0;i<64;i++) {
  1804. int j= s->dsp.idct_permutation[i];
  1805. v = ff_mpeg1_default_non_intra_matrix[i];
  1806. s->inter_matrix[j] = v;
  1807. s->chroma_inter_matrix[j] = v;
  1808. }
  1809. }
  1810. if(show_bits(&s->gb, 23) != 0){
  1811. av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
  1812. return -1;
  1813. }
  1814. /* we set MPEG-2 parameters so that it emulates MPEG-1 */
  1815. s->progressive_sequence = 1;
  1816. s->progressive_frame = 1;
  1817. s->picture_structure = PICT_FRAME;
  1818. s->frame_pred_frame_dct = 1;
  1819. s->chroma_format = 1;
  1820. s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG1VIDEO;
  1821. avctx->sub_id = 1; /* indicates MPEG-1 */
  1822. s->out_format = FMT_MPEG1;
  1823. s->swap_uv = 0;//AFAIK VCR2 does not have SEQ_HEADER
  1824. if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
  1825. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1826. av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
  1827. s->avctx->rc_buffer_size, s->bit_rate);
  1828. return 0;
  1829. }
  1830. static int vcr2_init_sequence(AVCodecContext *avctx)
  1831. {
  1832. Mpeg1Context *s1 = avctx->priv_data;
  1833. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1834. int i, v;
  1835. /* start new MPEG-1 context decoding */
  1836. s->out_format = FMT_MPEG1;
  1837. if (s1->mpeg_enc_ctx_allocated) {
  1838. MPV_common_end(s);
  1839. }
  1840. s->width = avctx->coded_width;
  1841. s->height = avctx->coded_height;
  1842. avctx->has_b_frames= 0; //true?
  1843. s->low_delay= 1;
  1844. avctx->pix_fmt = mpeg_get_pixelformat(avctx);
  1845. if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT )
  1846. if( avctx->idct_algo == FF_IDCT_AUTO )
  1847. avctx->idct_algo = FF_IDCT_SIMPLE;
  1848. if (MPV_common_init(s) < 0)
  1849. return -1;
  1850. exchange_uv(s);//common init reset pblocks, so we swap them here
  1851. s->swap_uv = 1;// in case of xvmc we need to swap uv for each MB
  1852. s1->mpeg_enc_ctx_allocated = 1;
  1853. for(i=0;i<64;i++) {
  1854. int j= s->dsp.idct_permutation[i];
  1855. v = ff_mpeg1_default_intra_matrix[i];
  1856. s->intra_matrix[j] = v;
  1857. s->chroma_intra_matrix[j] = v;
  1858. v = ff_mpeg1_default_non_intra_matrix[i];
  1859. s->inter_matrix[j] = v;
  1860. s->chroma_inter_matrix[j] = v;
  1861. }
  1862. s->progressive_sequence = 1;
  1863. s->progressive_frame = 1;
  1864. s->picture_structure = PICT_FRAME;
  1865. s->frame_pred_frame_dct = 1;
  1866. s->chroma_format = 1;
  1867. s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
  1868. avctx->sub_id = 2; /* indicates MPEG-2 */
  1869. return 0;
  1870. }
  1871. static void mpeg_decode_user_data(AVCodecContext *avctx,
  1872. const uint8_t *buf, int buf_size)
  1873. {
  1874. const uint8_t *p;
  1875. int len, flags;
  1876. p = buf;
  1877. len = buf_size;
  1878. /* we parse the DTG active format information */
  1879. if (len >= 5 &&
  1880. p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
  1881. flags = p[4];
  1882. p += 5;
  1883. len -= 5;
  1884. if (flags & 0x80) {
  1885. /* skip event id */
  1886. if (len < 2)
  1887. return;
  1888. p += 2;
  1889. len -= 2;
  1890. }
  1891. if (flags & 0x40) {
  1892. if (len < 1)
  1893. return;
  1894. avctx->dtg_active_format = p[0] & 0x0f;
  1895. }
  1896. }
  1897. }
  1898. static void mpeg_decode_gop(AVCodecContext *avctx,
  1899. const uint8_t *buf, int buf_size){
  1900. Mpeg1Context *s1 = avctx->priv_data;
  1901. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1902. int drop_frame_flag;
  1903. int time_code_hours, time_code_minutes;
  1904. int time_code_seconds, time_code_pictures;
  1905. int closed_gop, broken_link;
  1906. init_get_bits(&s->gb, buf, buf_size*8);
  1907. drop_frame_flag = get_bits1(&s->gb);
  1908. time_code_hours=get_bits(&s->gb,5);
  1909. time_code_minutes = get_bits(&s->gb,6);
  1910. skip_bits1(&s->gb);//marker bit
  1911. time_code_seconds = get_bits(&s->gb,6);
  1912. time_code_pictures = get_bits(&s->gb,6);
  1913. closed_gop = get_bits1(&s->gb);
  1914. /*broken_link indicate that after editing the
  1915. reference frames of the first B-Frames after GOP I-Frame
  1916. are missing (open gop)*/
  1917. broken_link = get_bits1(&s->gb);
  1918. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1919. av_log(s->avctx, AV_LOG_DEBUG, "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
  1920. time_code_hours, time_code_minutes, time_code_seconds,
  1921. time_code_pictures, closed_gop, broken_link);
  1922. }
  1923. /**
  1924. * Finds the end of the current frame in the bitstream.
  1925. * @return the position of the first byte of the next frame, or -1
  1926. */
  1927. int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
  1928. {
  1929. int i;
  1930. uint32_t state= pc->state;
  1931. /* EOF considered as end of frame */
  1932. if (buf_size == 0)
  1933. return 0;
  1934. /*
  1935. 0 frame start -> 1/4
  1936. 1 first_SEQEXT -> 0/2
  1937. 2 first field start -> 3/0
  1938. 3 second_SEQEXT -> 2/0
  1939. 4 searching end
  1940. */
  1941. for(i=0; i<buf_size; i++){
  1942. assert(pc->frame_start_found>=0 && pc->frame_start_found<=4);
  1943. if(pc->frame_start_found&1){
  1944. if(state == EXT_START_CODE && (buf[i]&0xF0) != 0x80)
  1945. pc->frame_start_found--;
  1946. else if(state == EXT_START_CODE+2){
  1947. if((buf[i]&3) == 3) pc->frame_start_found= 0;
  1948. else pc->frame_start_found= (pc->frame_start_found+1)&3;
  1949. }
  1950. state++;
  1951. }else{
  1952. i= ff_find_start_code(buf+i, buf+buf_size, &state) - buf - 1;
  1953. if(pc->frame_start_found==0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE){
  1954. i++;
  1955. pc->frame_start_found=4;
  1956. }
  1957. if(state == SEQ_END_CODE){
  1958. pc->state=-1;
  1959. return i+1;
  1960. }
  1961. if(pc->frame_start_found==2 && state == SEQ_START_CODE)
  1962. pc->frame_start_found= 0;
  1963. if(pc->frame_start_found<4 && state == EXT_START_CODE)
  1964. pc->frame_start_found++;
  1965. if(pc->frame_start_found == 4 && (state&0xFFFFFF00) == 0x100){
  1966. if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
  1967. pc->frame_start_found=0;
  1968. pc->state=-1;
  1969. return i-3;
  1970. }
  1971. }
  1972. }
  1973. }
  1974. pc->state= state;
  1975. return END_NOT_FOUND;
  1976. }
  1977. static int decode_chunks(AVCodecContext *avctx,
  1978. AVFrame *picture, int *data_size,
  1979. const uint8_t *buf, int buf_size);
  1980. /* handle buffering and image synchronisation */
  1981. static int mpeg_decode_frame(AVCodecContext *avctx,
  1982. void *data, int *data_size,
  1983. const uint8_t *buf, int buf_size)
  1984. {
  1985. Mpeg1Context *s = avctx->priv_data;
  1986. AVFrame *picture = data;
  1987. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  1988. dprintf(avctx, "fill_buffer\n");
  1989. if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
  1990. /* special case for last picture */
  1991. if (s2->low_delay==0 && s2->next_picture_ptr) {
  1992. *picture= *(AVFrame*)s2->next_picture_ptr;
  1993. s2->next_picture_ptr= NULL;
  1994. *data_size = sizeof(AVFrame);
  1995. }
  1996. return buf_size;
  1997. }
  1998. if(s2->flags&CODEC_FLAG_TRUNCATED){
  1999. int next= ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size);
  2000. if( ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0 )
  2001. return buf_size;
  2002. }
  2003. #if 0
  2004. if (s->repeat_field % 2 == 1) {
  2005. s->repeat_field++;
  2006. //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
  2007. // s2->picture_number, s->repeat_field);
  2008. if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
  2009. *data_size = sizeof(AVPicture);
  2010. goto the_end;
  2011. }
  2012. }
  2013. #endif
  2014. if(s->mpeg_enc_ctx_allocated==0 && avctx->codec_tag == ff_get_fourcc("VCR2"))
  2015. vcr2_init_sequence(avctx);
  2016. s->slice_count= 0;
  2017. if(avctx->extradata && !avctx->frame_number)
  2018. decode_chunks(avctx, picture, data_size, avctx->extradata, avctx->extradata_size);
  2019. return decode_chunks(avctx, picture, data_size, buf, buf_size);
  2020. }
  2021. static int decode_chunks(AVCodecContext *avctx,
  2022. AVFrame *picture, int *data_size,
  2023. const uint8_t *buf, int buf_size)
  2024. {
  2025. Mpeg1Context *s = avctx->priv_data;
  2026. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  2027. const uint8_t *buf_ptr = buf;
  2028. const uint8_t *buf_end = buf + buf_size;
  2029. int ret, input_size;
  2030. for(;;) {
  2031. /* find next start code */
  2032. uint32_t start_code = -1;
  2033. buf_ptr = ff_find_start_code(buf_ptr,buf_end, &start_code);
  2034. if (start_code > 0x1ff){
  2035. if(s2->pict_type != FF_B_TYPE || avctx->skip_frame <= AVDISCARD_DEFAULT){
  2036. if(avctx->thread_count > 1){
  2037. int i;
  2038. avctx->execute(avctx, slice_decode_thread, (void**)&(s2->thread_context[0]), NULL, s->slice_count, sizeof(void*));
  2039. for(i=0; i<s->slice_count; i++)
  2040. s2->error_count += s2->thread_context[i]->error_count;
  2041. }
  2042. if (slice_end(avctx, picture)) {
  2043. if(s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
  2044. *data_size = sizeof(AVPicture);
  2045. }
  2046. }
  2047. s2->pict_type= 0;
  2048. return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
  2049. }
  2050. input_size = buf_end - buf_ptr;
  2051. if(avctx->debug & FF_DEBUG_STARTCODE){
  2052. av_log(avctx, AV_LOG_DEBUG, "%3X at %td left %d\n", start_code, buf_ptr-buf, input_size);
  2053. }
  2054. /* prepare data for next start code */
  2055. switch(start_code) {
  2056. case SEQ_START_CODE:
  2057. mpeg1_decode_sequence(avctx, buf_ptr,
  2058. input_size);
  2059. break;
  2060. case PICTURE_START_CODE:
  2061. /* we have a complete image: we try to decompress it */
  2062. if(mpeg1_decode_picture(avctx,
  2063. buf_ptr, input_size) < 0)
  2064. s2->pict_type=0;
  2065. break;
  2066. case EXT_START_CODE:
  2067. mpeg_decode_extension(avctx,
  2068. buf_ptr, input_size);
  2069. break;
  2070. case USER_START_CODE:
  2071. mpeg_decode_user_data(avctx,
  2072. buf_ptr, input_size);
  2073. break;
  2074. case GOP_START_CODE:
  2075. s2->first_field=0;
  2076. mpeg_decode_gop(avctx,
  2077. buf_ptr, input_size);
  2078. break;
  2079. default:
  2080. if (start_code >= SLICE_MIN_START_CODE &&
  2081. start_code <= SLICE_MAX_START_CODE) {
  2082. int mb_y= start_code - SLICE_MIN_START_CODE;
  2083. if(s2->last_picture_ptr==NULL){
  2084. /* Skip B-frames if we do not have reference frames. */
  2085. if(s2->pict_type==FF_B_TYPE) break;
  2086. }
  2087. if(s2->next_picture_ptr==NULL){
  2088. /* Skip P-frames if we do not have a reference frame or we have an invalid header. */
  2089. if(s2->pict_type==FF_P_TYPE && (s2->first_field || s2->picture_structure==PICT_FRAME)) break;
  2090. }
  2091. /* Skip B-frames if we are in a hurry. */
  2092. if(avctx->hurry_up && s2->pict_type==FF_B_TYPE) break;
  2093. if( (avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type==FF_B_TYPE)
  2094. ||(avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type!=FF_I_TYPE)
  2095. || avctx->skip_frame >= AVDISCARD_ALL)
  2096. break;
  2097. /* Skip everything if we are in a hurry>=5. */
  2098. if(avctx->hurry_up>=5) break;
  2099. if (!s->mpeg_enc_ctx_allocated) break;
  2100. if(s2->codec_id == CODEC_ID_MPEG2VIDEO){
  2101. if(mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
  2102. break;
  2103. }
  2104. if(!s2->pict_type){
  2105. av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
  2106. break;
  2107. }
  2108. if(s2->first_slice){
  2109. s2->first_slice=0;
  2110. if(mpeg_field_start(s2) < 0)
  2111. return -1;
  2112. }
  2113. if(!s2->current_picture_ptr){
  2114. av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\n");
  2115. return -1;
  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. #ifdef CONFIG_XVMC
  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