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.

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