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.

2577 lines
87KB

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