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.

2607 lines
90KB

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