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.

2581 lines
87KB

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