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.

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