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.

2683 lines
93KB

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