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.

2545 lines
86KB

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