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.

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