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.

2617 lines
89KB

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