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.

2634 lines
91KB

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