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.

2687 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 == AV_PICTURE_TYPE_P) {
  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 AV_PICTURE_TYPE_I:
  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 AV_PICTURE_TYPE_P:
  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 AV_PICTURE_TYPE_B:
  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 == AV_PICTURE_TYPE_B || 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. #if CONFIG_MPEG_XVMC_DECODER
  1093. PIX_FMT_XVMC_MPEG2_IDCT,
  1094. PIX_FMT_XVMC_MPEG2_MC,
  1095. #endif
  1096. #if CONFIG_MPEG1_VDPAU_HWACCEL
  1097. PIX_FMT_VDPAU_MPEG1,
  1098. #endif
  1099. PIX_FMT_YUV420P,
  1100. PIX_FMT_NONE
  1101. };
  1102. static const enum PixelFormat mpeg2_hwaccel_pixfmt_list_420[] = {
  1103. #if CONFIG_MPEG_XVMC_DECODER
  1104. PIX_FMT_XVMC_MPEG2_IDCT,
  1105. PIX_FMT_XVMC_MPEG2_MC,
  1106. #endif
  1107. #if CONFIG_MPEG2_VDPAU_HWACCEL
  1108. PIX_FMT_VDPAU_MPEG2,
  1109. #endif
  1110. #if CONFIG_MPEG2_DXVA2_HWACCEL
  1111. PIX_FMT_DXVA2_VLD,
  1112. #endif
  1113. #if CONFIG_MPEG2_VAAPI_HWACCEL
  1114. PIX_FMT_VAAPI_VLD,
  1115. #endif
  1116. PIX_FMT_YUV420P,
  1117. PIX_FMT_NONE
  1118. };
  1119. static inline int uses_vdpau(AVCodecContext *avctx) {
  1120. return avctx->pix_fmt == PIX_FMT_VDPAU_MPEG1 || avctx->pix_fmt == PIX_FMT_VDPAU_MPEG2;
  1121. }
  1122. static enum PixelFormat mpeg_get_pixelformat(AVCodecContext *avctx){
  1123. Mpeg1Context *s1 = avctx->priv_data;
  1124. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1125. if(s->chroma_format < 2) {
  1126. enum PixelFormat res;
  1127. res = avctx->get_format(avctx,
  1128. avctx->codec_id == CODEC_ID_MPEG1VIDEO ?
  1129. mpeg1_hwaccel_pixfmt_list_420 :
  1130. mpeg2_hwaccel_pixfmt_list_420);
  1131. if (res != PIX_FMT_XVMC_MPEG2_IDCT && res != PIX_FMT_XVMC_MPEG2_MC) {
  1132. avctx->xvmc_acceleration = 0;
  1133. } else if (!avctx->xvmc_acceleration) {
  1134. avctx->xvmc_acceleration = 2;
  1135. }
  1136. return res;
  1137. } else if(s->chroma_format == 2)
  1138. return PIX_FMT_YUV422P;
  1139. else
  1140. return PIX_FMT_YUV444P;
  1141. }
  1142. /* Call this function when we know all parameters.
  1143. * It may be called in different places for MPEG-1 and MPEG-2. */
  1144. static int mpeg_decode_postinit(AVCodecContext *avctx){
  1145. Mpeg1Context *s1 = avctx->priv_data;
  1146. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1147. uint8_t old_permutation[64];
  1148. if (
  1149. (s1->mpeg_enc_ctx_allocated == 0)||
  1150. avctx->coded_width != s->width ||
  1151. avctx->coded_height != s->height||
  1152. s1->save_width != s->width ||
  1153. s1->save_height != s->height ||
  1154. s1->save_aspect_info != s->aspect_ratio_info||
  1155. s1->save_progressive_seq != s->progressive_sequence ||
  1156. 0)
  1157. {
  1158. if (s1->mpeg_enc_ctx_allocated) {
  1159. ParseContext pc= s->parse_context;
  1160. s->parse_context.buffer=0;
  1161. MPV_common_end(s);
  1162. s->parse_context= pc;
  1163. }
  1164. if( (s->width == 0 )||(s->height == 0))
  1165. return -2;
  1166. avcodec_set_dimensions(avctx, s->width, s->height);
  1167. avctx->bit_rate = s->bit_rate;
  1168. s1->save_aspect_info = s->aspect_ratio_info;
  1169. s1->save_width = s->width;
  1170. s1->save_height = s->height;
  1171. s1->save_progressive_seq = s->progressive_sequence;
  1172. /* low_delay may be forced, in this case we will have B-frames
  1173. * that behave like P-frames. */
  1174. avctx->has_b_frames = !(s->low_delay);
  1175. assert((avctx->sub_id==1) == (avctx->codec_id==CODEC_ID_MPEG1VIDEO));
  1176. if(avctx->codec_id==CODEC_ID_MPEG1VIDEO){
  1177. //MPEG-1 fps
  1178. avctx->time_base.den= ff_frame_rate_tab[s->frame_rate_index].num;
  1179. avctx->time_base.num= ff_frame_rate_tab[s->frame_rate_index].den;
  1180. //MPEG-1 aspect
  1181. avctx->sample_aspect_ratio= av_d2q(
  1182. 1.0/ff_mpeg1_aspect[s->aspect_ratio_info], 255);
  1183. avctx->ticks_per_frame=1;
  1184. }else{//MPEG-2
  1185. //MPEG-2 fps
  1186. av_reduce(
  1187. &s->avctx->time_base.den,
  1188. &s->avctx->time_base.num,
  1189. ff_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num*2,
  1190. ff_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
  1191. 1<<30);
  1192. avctx->ticks_per_frame=2;
  1193. //MPEG-2 aspect
  1194. if(s->aspect_ratio_info > 1){
  1195. AVRational dar=
  1196. av_mul_q(
  1197. av_div_q(
  1198. ff_mpeg2_aspect[s->aspect_ratio_info],
  1199. (AVRational){s1->pan_scan.width, s1->pan_scan.height}
  1200. ),
  1201. (AVRational){s->width, s->height});
  1202. //we ignore the spec here and guess a bit as reality does not match the spec, see for example
  1203. // res_change_ffmpeg_aspect.ts and sequence-display-aspect.mpg
  1204. //issue1613, 621, 562
  1205. if( (s1->pan_scan.width == 0 )||(s1->pan_scan.height == 0)
  1206. || (av_cmp_q(dar,(AVRational){4,3})&&av_cmp_q(dar,(AVRational){16,9}))){
  1207. s->avctx->sample_aspect_ratio=
  1208. av_div_q(
  1209. ff_mpeg2_aspect[s->aspect_ratio_info],
  1210. (AVRational){s->width, s->height}
  1211. );
  1212. }else{
  1213. s->avctx->sample_aspect_ratio=
  1214. av_div_q(
  1215. ff_mpeg2_aspect[s->aspect_ratio_info],
  1216. (AVRational){s1->pan_scan.width, s1->pan_scan.height}
  1217. );
  1218. //issue1613 4/3 16/9 -> 16/9
  1219. //res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
  1220. //widescreen-issue562.mpg 4/3 16/9 -> 16/9
  1221. // s->avctx->sample_aspect_ratio= av_mul_q(s->avctx->sample_aspect_ratio, (AVRational){s->width, s->height});
  1222. //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);
  1223. //av_log(NULL, AV_LOG_ERROR, "B %d/%d\n",s->avctx->sample_aspect_ratio.num, s->avctx->sample_aspect_ratio.den);
  1224. }
  1225. }else{
  1226. s->avctx->sample_aspect_ratio=
  1227. ff_mpeg2_aspect[s->aspect_ratio_info];
  1228. }
  1229. }//MPEG-2
  1230. avctx->pix_fmt = mpeg_get_pixelformat(avctx);
  1231. avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
  1232. //until then pix_fmt may be changed right after codec init
  1233. if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT ||
  1234. avctx->hwaccel )
  1235. if( avctx->idct_algo == FF_IDCT_AUTO )
  1236. avctx->idct_algo = FF_IDCT_SIMPLE;
  1237. /* Quantization matrices may need reordering
  1238. * if DCT permutation is changed. */
  1239. memcpy(old_permutation,s->dsp.idct_permutation,64*sizeof(uint8_t));
  1240. if (MPV_common_init(s) < 0)
  1241. return -2;
  1242. quant_matrix_rebuild(s->intra_matrix, old_permutation,s->dsp.idct_permutation);
  1243. quant_matrix_rebuild(s->inter_matrix, old_permutation,s->dsp.idct_permutation);
  1244. quant_matrix_rebuild(s->chroma_intra_matrix,old_permutation,s->dsp.idct_permutation);
  1245. quant_matrix_rebuild(s->chroma_inter_matrix,old_permutation,s->dsp.idct_permutation);
  1246. s1->mpeg_enc_ctx_allocated = 1;
  1247. }
  1248. return 0;
  1249. }
  1250. static int mpeg1_decode_picture(AVCodecContext *avctx,
  1251. const uint8_t *buf, int buf_size)
  1252. {
  1253. Mpeg1Context *s1 = avctx->priv_data;
  1254. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1255. int ref, f_code, vbv_delay;
  1256. init_get_bits(&s->gb, buf, buf_size*8);
  1257. ref = get_bits(&s->gb, 10); /* temporal ref */
  1258. s->pict_type = get_bits(&s->gb, 3);
  1259. if(s->pict_type == 0 || s->pict_type > 3)
  1260. return -1;
  1261. vbv_delay= get_bits(&s->gb, 16);
  1262. if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type == AV_PICTURE_TYPE_B) {
  1263. s->full_pel[0] = get_bits1(&s->gb);
  1264. f_code = get_bits(&s->gb, 3);
  1265. if (f_code == 0 && avctx->error_recognition >= FF_ER_COMPLIANT)
  1266. return -1;
  1267. s->mpeg_f_code[0][0] = f_code;
  1268. s->mpeg_f_code[0][1] = f_code;
  1269. }
  1270. if (s->pict_type == AV_PICTURE_TYPE_B) {
  1271. s->full_pel[1] = get_bits1(&s->gb);
  1272. f_code = get_bits(&s->gb, 3);
  1273. if (f_code == 0 && avctx->error_recognition >= FF_ER_COMPLIANT)
  1274. return -1;
  1275. s->mpeg_f_code[1][0] = f_code;
  1276. s->mpeg_f_code[1][1] = f_code;
  1277. }
  1278. s->current_picture.pict_type= s->pict_type;
  1279. s->current_picture.key_frame= s->pict_type == AV_PICTURE_TYPE_I;
  1280. if(avctx->debug & FF_DEBUG_PICT_INFO)
  1281. av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
  1282. s->y_dc_scale = 8;
  1283. s->c_dc_scale = 8;
  1284. return 0;
  1285. }
  1286. static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
  1287. {
  1288. MpegEncContext *s= &s1->mpeg_enc_ctx;
  1289. int horiz_size_ext, vert_size_ext;
  1290. int bit_rate_ext;
  1291. skip_bits(&s->gb, 1); /* profile and level esc*/
  1292. s->avctx->profile= get_bits(&s->gb, 3);
  1293. s->avctx->level= get_bits(&s->gb, 4);
  1294. s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
  1295. s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
  1296. horiz_size_ext = get_bits(&s->gb, 2);
  1297. vert_size_ext = get_bits(&s->gb, 2);
  1298. s->width |= (horiz_size_ext << 12);
  1299. s->height |= (vert_size_ext << 12);
  1300. bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
  1301. s->bit_rate += (bit_rate_ext << 18) * 400;
  1302. skip_bits1(&s->gb); /* marker */
  1303. s->avctx->rc_buffer_size += get_bits(&s->gb, 8)*1024*16<<10;
  1304. s->low_delay = get_bits1(&s->gb);
  1305. if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
  1306. s1->frame_rate_ext.num = get_bits(&s->gb, 2)+1;
  1307. s1->frame_rate_ext.den = get_bits(&s->gb, 5)+1;
  1308. av_dlog(s->avctx, "sequence extension\n");
  1309. s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
  1310. s->avctx->sub_id = 2; /* indicates MPEG-2 found */
  1311. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1312. av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
  1313. s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
  1314. }
  1315. static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
  1316. {
  1317. MpegEncContext *s= &s1->mpeg_enc_ctx;
  1318. int color_description, w, h;
  1319. skip_bits(&s->gb, 3); /* video format */
  1320. color_description= get_bits1(&s->gb);
  1321. if(color_description){
  1322. s->avctx->color_primaries= get_bits(&s->gb, 8);
  1323. s->avctx->color_trc = get_bits(&s->gb, 8);
  1324. s->avctx->colorspace = get_bits(&s->gb, 8);
  1325. }
  1326. w= get_bits(&s->gb, 14);
  1327. skip_bits(&s->gb, 1); //marker
  1328. h= get_bits(&s->gb, 14);
  1329. // remaining 3 bits are zero padding
  1330. s1->pan_scan.width= 16*w;
  1331. s1->pan_scan.height=16*h;
  1332. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1333. av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
  1334. }
  1335. static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
  1336. {
  1337. MpegEncContext *s= &s1->mpeg_enc_ctx;
  1338. int i,nofco;
  1339. nofco = 1;
  1340. if(s->progressive_sequence){
  1341. if(s->repeat_first_field){
  1342. nofco++;
  1343. if(s->top_field_first)
  1344. nofco++;
  1345. }
  1346. }else{
  1347. if(s->picture_structure == PICT_FRAME){
  1348. nofco++;
  1349. if(s->repeat_first_field)
  1350. nofco++;
  1351. }
  1352. }
  1353. for(i=0; i<nofco; i++){
  1354. s1->pan_scan.position[i][0]= get_sbits(&s->gb, 16);
  1355. skip_bits(&s->gb, 1); //marker
  1356. s1->pan_scan.position[i][1]= get_sbits(&s->gb, 16);
  1357. skip_bits(&s->gb, 1); //marker
  1358. }
  1359. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1360. av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
  1361. s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
  1362. s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
  1363. s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]
  1364. );
  1365. }
  1366. static int load_matrix(MpegEncContext *s, uint16_t matrix0[64], uint16_t matrix1[64], int intra){
  1367. int i;
  1368. for(i=0; i<64; i++) {
  1369. int j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
  1370. int v = get_bits(&s->gb, 8);
  1371. if(v==0){
  1372. av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
  1373. return -1;
  1374. }
  1375. if(intra && i==0 && v!=8){
  1376. av_log(s->avctx, AV_LOG_ERROR, "intra matrix invalid, ignoring\n");
  1377. v= 8; // needed by pink.mpg / issue1046
  1378. }
  1379. matrix0[j] = v;
  1380. if(matrix1)
  1381. matrix1[j] = v;
  1382. }
  1383. return 0;
  1384. }
  1385. static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
  1386. {
  1387. av_dlog(s->avctx, "matrix extension\n");
  1388. if(get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
  1389. if(get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
  1390. if(get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, NULL , 1);
  1391. if(get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, NULL , 0);
  1392. }
  1393. static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
  1394. {
  1395. MpegEncContext *s= &s1->mpeg_enc_ctx;
  1396. s->full_pel[0] = s->full_pel[1] = 0;
  1397. s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
  1398. s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
  1399. s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
  1400. s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
  1401. if(!s->pict_type && s1->mpeg_enc_ctx_allocated){
  1402. av_log(s->avctx, AV_LOG_ERROR, "Missing picture start code, guessing missing values\n");
  1403. if(s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1]==15){
  1404. if(s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
  1405. s->pict_type= AV_PICTURE_TYPE_I;
  1406. else
  1407. s->pict_type= AV_PICTURE_TYPE_P;
  1408. }else
  1409. s->pict_type= AV_PICTURE_TYPE_B;
  1410. s->current_picture.pict_type= s->pict_type;
  1411. s->current_picture.key_frame= s->pict_type == AV_PICTURE_TYPE_I;
  1412. }
  1413. s->intra_dc_precision = get_bits(&s->gb, 2);
  1414. s->picture_structure = get_bits(&s->gb, 2);
  1415. s->top_field_first = get_bits1(&s->gb);
  1416. s->frame_pred_frame_dct = get_bits1(&s->gb);
  1417. s->concealment_motion_vectors = get_bits1(&s->gb);
  1418. s->q_scale_type = get_bits1(&s->gb);
  1419. s->intra_vlc_format = get_bits1(&s->gb);
  1420. s->alternate_scan = get_bits1(&s->gb);
  1421. s->repeat_first_field = get_bits1(&s->gb);
  1422. s->chroma_420_type = get_bits1(&s->gb);
  1423. s->progressive_frame = get_bits1(&s->gb);
  1424. if(s->progressive_sequence && !s->progressive_frame){
  1425. s->progressive_frame= 1;
  1426. av_log(s->avctx, AV_LOG_ERROR, "interlaced frame in progressive sequence, ignoring\n");
  1427. }
  1428. if(s->picture_structure==0 || (s->progressive_frame && s->picture_structure!=PICT_FRAME)){
  1429. av_log(s->avctx, AV_LOG_ERROR, "picture_structure %d invalid, ignoring\n", s->picture_structure);
  1430. s->picture_structure= PICT_FRAME;
  1431. }
  1432. if(s->progressive_sequence && !s->frame_pred_frame_dct){
  1433. av_log(s->avctx, AV_LOG_ERROR, "invalid frame_pred_frame_dct\n");
  1434. }
  1435. if(s->picture_structure == PICT_FRAME){
  1436. s->first_field=0;
  1437. s->v_edge_pos= 16*s->mb_height;
  1438. }else{
  1439. s->first_field ^= 1;
  1440. s->v_edge_pos= 8*s->mb_height;
  1441. memset(s->mbskip_table, 0, s->mb_stride*s->mb_height);
  1442. }
  1443. if(s->alternate_scan){
  1444. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
  1445. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
  1446. }else{
  1447. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
  1448. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
  1449. }
  1450. /* composite display not parsed */
  1451. av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
  1452. av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
  1453. av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
  1454. av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
  1455. av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
  1456. av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
  1457. av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
  1458. av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
  1459. av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
  1460. }
  1461. static void exchange_uv(MpegEncContext *s){
  1462. DCTELEM (*tmp)[64];
  1463. tmp = s->pblocks[4];
  1464. s->pblocks[4] = s->pblocks[5];
  1465. s->pblocks[5] = tmp;
  1466. }
  1467. static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size){
  1468. AVCodecContext *avctx= s->avctx;
  1469. Mpeg1Context *s1 = (Mpeg1Context*)s;
  1470. /* start frame decoding */
  1471. if(s->first_field || s->picture_structure==PICT_FRAME){
  1472. if(MPV_frame_start(s, avctx) < 0)
  1473. return -1;
  1474. ff_er_frame_start(s);
  1475. /* first check if we must repeat the frame */
  1476. s->current_picture_ptr->repeat_pict = 0;
  1477. if (s->repeat_first_field) {
  1478. if (s->progressive_sequence) {
  1479. if (s->top_field_first)
  1480. s->current_picture_ptr->repeat_pict = 4;
  1481. else
  1482. s->current_picture_ptr->repeat_pict = 2;
  1483. } else if (s->progressive_frame) {
  1484. s->current_picture_ptr->repeat_pict = 1;
  1485. }
  1486. }
  1487. *s->current_picture_ptr->pan_scan= s1->pan_scan;
  1488. if (HAVE_PTHREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
  1489. ff_thread_finish_setup(avctx);
  1490. }else{ //second field
  1491. int i;
  1492. if(!s->current_picture_ptr){
  1493. av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
  1494. return -1;
  1495. }
  1496. for(i=0; i<4; i++){
  1497. s->current_picture.data[i] = s->current_picture_ptr->data[i];
  1498. if(s->picture_structure == PICT_BOTTOM_FIELD){
  1499. s->current_picture.data[i] += s->current_picture_ptr->linesize[i];
  1500. }
  1501. }
  1502. }
  1503. if (avctx->hwaccel) {
  1504. if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
  1505. return -1;
  1506. }
  1507. // MPV_frame_start will call this function too,
  1508. // but we need to call it on every field
  1509. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1510. if(ff_xvmc_field_start(s,avctx) < 0)
  1511. return -1;
  1512. return 0;
  1513. }
  1514. #define DECODE_SLICE_ERROR -1
  1515. #define DECODE_SLICE_OK 0
  1516. /**
  1517. * decodes a slice. MpegEncContext.mb_y must be set to the MB row from the startcode
  1518. * @return DECODE_SLICE_ERROR if the slice is damaged<br>
  1519. * DECODE_SLICE_OK if this slice is ok<br>
  1520. */
  1521. static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y,
  1522. const uint8_t **buf, int buf_size)
  1523. {
  1524. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1525. AVCodecContext *avctx= s->avctx;
  1526. const int field_pic= s->picture_structure != PICT_FRAME;
  1527. const int lowres= s->avctx->lowres;
  1528. s->resync_mb_x=
  1529. s->resync_mb_y= -1;
  1530. assert(mb_y < s->mb_height);
  1531. init_get_bits(&s->gb, *buf, buf_size*8);
  1532. ff_mpeg1_clean_buffers(s);
  1533. s->interlaced_dct = 0;
  1534. s->qscale = get_qscale(s);
  1535. if(s->qscale == 0){
  1536. av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
  1537. return -1;
  1538. }
  1539. /* extra slice info */
  1540. while (get_bits1(&s->gb) != 0) {
  1541. skip_bits(&s->gb, 8);
  1542. }
  1543. s->mb_x=0;
  1544. if(mb_y==0 && s->codec_tag == AV_RL32("SLIF")){
  1545. skip_bits1(&s->gb);
  1546. }else{
  1547. for(;;) {
  1548. int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  1549. if (code < 0){
  1550. av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
  1551. return -1;
  1552. }
  1553. if (code >= 33) {
  1554. if (code == 33) {
  1555. s->mb_x += 33;
  1556. }
  1557. /* otherwise, stuffing, nothing to do */
  1558. } else {
  1559. s->mb_x += code;
  1560. break;
  1561. }
  1562. }
  1563. }
  1564. if(s->mb_x >= (unsigned)s->mb_width){
  1565. av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
  1566. return -1;
  1567. }
  1568. if (avctx->hwaccel) {
  1569. const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
  1570. int start_code = -1;
  1571. buf_end = ff_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
  1572. if (buf_end < *buf + buf_size)
  1573. buf_end -= 4;
  1574. s->mb_y = mb_y;
  1575. if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
  1576. return DECODE_SLICE_ERROR;
  1577. *buf = buf_end;
  1578. return DECODE_SLICE_OK;
  1579. }
  1580. s->resync_mb_x= s->mb_x;
  1581. s->resync_mb_y= s->mb_y= mb_y;
  1582. s->mb_skip_run= 0;
  1583. ff_init_block_index(s);
  1584. if (s->mb_y==0 && s->mb_x==0 && (s->first_field || s->picture_structure==PICT_FRAME)) {
  1585. if(s->avctx->debug&FF_DEBUG_PICT_INFO){
  1586. 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",
  1587. 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],
  1588. s->pict_type == AV_PICTURE_TYPE_I ? "I" : (s->pict_type == AV_PICTURE_TYPE_P ? "P" : (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
  1589. s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
  1590. s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
  1591. s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
  1592. }
  1593. }
  1594. for(;;) {
  1595. //If 1, we memcpy blocks in xvmcvideo.
  1596. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
  1597. ff_xvmc_init_block(s);//set s->block
  1598. if(mpeg_decode_mb(s, s->block) < 0)
  1599. return -1;
  1600. if(s->current_picture.motion_val[0] && !s->encoding){ //note motion_val is normally NULL unless we want to extract the MVs
  1601. const int wrap = s->b8_stride;
  1602. int xy = s->mb_x*2 + s->mb_y*2*wrap;
  1603. int b8_xy= 4*(s->mb_x + s->mb_y*s->mb_stride);
  1604. int motion_x, motion_y, dir, i;
  1605. for(i=0; i<2; i++){
  1606. for(dir=0; dir<2; dir++){
  1607. if (s->mb_intra || (dir==1 && s->pict_type != AV_PICTURE_TYPE_B)) {
  1608. motion_x = motion_y = 0;
  1609. }else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)){
  1610. motion_x = s->mv[dir][0][0];
  1611. motion_y = s->mv[dir][0][1];
  1612. } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
  1613. motion_x = s->mv[dir][i][0];
  1614. motion_y = s->mv[dir][i][1];
  1615. }
  1616. s->current_picture.motion_val[dir][xy ][0] = motion_x;
  1617. s->current_picture.motion_val[dir][xy ][1] = motion_y;
  1618. s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
  1619. s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
  1620. s->current_picture.ref_index [dir][b8_xy ]=
  1621. s->current_picture.ref_index [dir][b8_xy + 1]= s->field_select[dir][i];
  1622. assert(s->field_select[dir][i]==0 || s->field_select[dir][i]==1);
  1623. }
  1624. xy += wrap;
  1625. b8_xy +=2;
  1626. }
  1627. }
  1628. s->dest[0] += 16 >> lowres;
  1629. s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
  1630. s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
  1631. MPV_decode_mb(s, s->block);
  1632. if (++s->mb_x >= s->mb_width) {
  1633. const int mb_size= 16>>s->avctx->lowres;
  1634. ff_draw_horiz_band(s, mb_size*(s->mb_y>>field_pic), mb_size);
  1635. MPV_report_decode_progress(s);
  1636. s->mb_x = 0;
  1637. s->mb_y += 1<<field_pic;
  1638. if(s->mb_y >= s->mb_height){
  1639. int left= get_bits_left(&s->gb);
  1640. int is_d10= s->chroma_format==2 && s->pict_type==AV_PICTURE_TYPE_I && avctx->profile==0 && avctx->level==5
  1641. && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
  1642. && s->progressive_frame == 0 /* vbv_delay == 0xBBB || 0xE10*/;
  1643. if(left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
  1644. || (avctx->error_recognition >= FF_ER_AGGRESSIVE && left>8)){
  1645. av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
  1646. return -1;
  1647. }else
  1648. goto eos;
  1649. }
  1650. ff_init_block_index(s);
  1651. }
  1652. /* skip mb handling */
  1653. if (s->mb_skip_run == -1) {
  1654. /* read increment again */
  1655. s->mb_skip_run = 0;
  1656. for(;;) {
  1657. int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  1658. if (code < 0){
  1659. av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
  1660. return -1;
  1661. }
  1662. if (code >= 33) {
  1663. if (code == 33) {
  1664. s->mb_skip_run += 33;
  1665. }else if(code == 35){
  1666. if(s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0){
  1667. av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
  1668. return -1;
  1669. }
  1670. goto eos; /* end of slice */
  1671. }
  1672. /* otherwise, stuffing, nothing to do */
  1673. } else {
  1674. s->mb_skip_run += code;
  1675. break;
  1676. }
  1677. }
  1678. if(s->mb_skip_run){
  1679. int i;
  1680. if(s->pict_type == AV_PICTURE_TYPE_I){
  1681. av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
  1682. return -1;
  1683. }
  1684. /* skip mb */
  1685. s->mb_intra = 0;
  1686. for(i=0;i<12;i++)
  1687. s->block_last_index[i] = -1;
  1688. if(s->picture_structure == PICT_FRAME)
  1689. s->mv_type = MV_TYPE_16X16;
  1690. else
  1691. s->mv_type = MV_TYPE_FIELD;
  1692. if (s->pict_type == AV_PICTURE_TYPE_P) {
  1693. /* if P type, zero motion vector is implied */
  1694. s->mv_dir = MV_DIR_FORWARD;
  1695. s->mv[0][0][0] = s->mv[0][0][1] = 0;
  1696. s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
  1697. s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
  1698. s->field_select[0][0]= (s->picture_structure - 1) & 1;
  1699. } else {
  1700. /* if B type, reuse previous vectors and directions */
  1701. s->mv[0][0][0] = s->last_mv[0][0][0];
  1702. s->mv[0][0][1] = s->last_mv[0][0][1];
  1703. s->mv[1][0][0] = s->last_mv[1][0][0];
  1704. s->mv[1][0][1] = s->last_mv[1][0][1];
  1705. }
  1706. }
  1707. }
  1708. }
  1709. eos: // end of slice
  1710. *buf += (get_bits_count(&s->gb)-1)/8;
  1711. //printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
  1712. return 0;
  1713. }
  1714. static int slice_decode_thread(AVCodecContext *c, void *arg){
  1715. MpegEncContext *s= *(void**)arg;
  1716. const uint8_t *buf= s->gb.buffer;
  1717. int mb_y= s->start_mb_y;
  1718. const int field_pic= s->picture_structure != PICT_FRAME;
  1719. s->error_count= (3*(s->end_mb_y - s->start_mb_y)*s->mb_width) >> field_pic;
  1720. for(;;){
  1721. uint32_t start_code;
  1722. int ret;
  1723. ret= mpeg_decode_slice((Mpeg1Context*)s, mb_y, &buf, s->gb.buffer_end - buf);
  1724. emms_c();
  1725. //av_log(c, AV_LOG_DEBUG, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
  1726. //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);
  1727. if(ret < 0){
  1728. if(s->resync_mb_x>=0 && s->resync_mb_y>=0)
  1729. 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);
  1730. }else{
  1731. 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);
  1732. }
  1733. if(s->mb_y == s->end_mb_y)
  1734. return 0;
  1735. start_code= -1;
  1736. buf = ff_find_start_code(buf, s->gb.buffer_end, &start_code);
  1737. mb_y= (start_code - SLICE_MIN_START_CODE) << field_pic;
  1738. if (s->picture_structure == PICT_BOTTOM_FIELD)
  1739. mb_y++;
  1740. if(mb_y < 0 || mb_y >= s->end_mb_y)
  1741. return -1;
  1742. }
  1743. return 0; //not reached
  1744. }
  1745. /**
  1746. * Handle slice ends.
  1747. * @return 1 if it seems to be the last slice
  1748. */
  1749. static int slice_end(AVCodecContext *avctx, AVFrame *pict)
  1750. {
  1751. Mpeg1Context *s1 = avctx->priv_data;
  1752. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1753. if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
  1754. return 0;
  1755. if (s->avctx->hwaccel) {
  1756. if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
  1757. av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
  1758. }
  1759. if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1760. ff_xvmc_field_end(s);
  1761. /* end of slice reached */
  1762. if (/*s->mb_y<<field_pic == s->mb_height &&*/ !s->first_field) {
  1763. /* end of image */
  1764. s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG2;
  1765. ff_er_frame_end(s);
  1766. MPV_frame_end(s);
  1767. if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
  1768. *pict= *(AVFrame*)s->current_picture_ptr;
  1769. ff_print_debug_info(s, pict);
  1770. } else {
  1771. if (avctx->active_thread_type & FF_THREAD_FRAME)
  1772. s->picture_number++;
  1773. /* latency of 1 frame for I- and P-frames */
  1774. /* XXX: use another variable than picture_number */
  1775. if (s->last_picture_ptr != NULL) {
  1776. *pict= *(AVFrame*)s->last_picture_ptr;
  1777. ff_print_debug_info(s, pict);
  1778. }
  1779. }
  1780. return 1;
  1781. } else {
  1782. return 0;
  1783. }
  1784. }
  1785. static int mpeg1_decode_sequence(AVCodecContext *avctx,
  1786. const uint8_t *buf, int buf_size)
  1787. {
  1788. Mpeg1Context *s1 = avctx->priv_data;
  1789. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1790. int width,height;
  1791. int i, v, j;
  1792. init_get_bits(&s->gb, buf, buf_size*8);
  1793. width = get_bits(&s->gb, 12);
  1794. height = get_bits(&s->gb, 12);
  1795. if (width <= 0 || height <= 0)
  1796. return -1;
  1797. s->aspect_ratio_info= get_bits(&s->gb, 4);
  1798. if (s->aspect_ratio_info == 0) {
  1799. av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
  1800. if (avctx->error_recognition >= FF_ER_COMPLIANT)
  1801. return -1;
  1802. }
  1803. s->frame_rate_index = get_bits(&s->gb, 4);
  1804. if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
  1805. return -1;
  1806. s->bit_rate = get_bits(&s->gb, 18) * 400;
  1807. if (get_bits1(&s->gb) == 0) /* marker */
  1808. return -1;
  1809. s->width = width;
  1810. s->height = height;
  1811. s->avctx->rc_buffer_size= get_bits(&s->gb, 10) * 1024*16;
  1812. skip_bits(&s->gb, 1);
  1813. /* get matrix */
  1814. if (get_bits1(&s->gb)) {
  1815. load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
  1816. } else {
  1817. for(i=0;i<64;i++) {
  1818. j = s->dsp.idct_permutation[i];
  1819. v = ff_mpeg1_default_intra_matrix[i];
  1820. s->intra_matrix[j] = v;
  1821. s->chroma_intra_matrix[j] = v;
  1822. }
  1823. }
  1824. if (get_bits1(&s->gb)) {
  1825. load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
  1826. } else {
  1827. for(i=0;i<64;i++) {
  1828. int j= s->dsp.idct_permutation[i];
  1829. v = ff_mpeg1_default_non_intra_matrix[i];
  1830. s->inter_matrix[j] = v;
  1831. s->chroma_inter_matrix[j] = v;
  1832. }
  1833. }
  1834. if(show_bits(&s->gb, 23) != 0){
  1835. av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
  1836. return -1;
  1837. }
  1838. /* we set MPEG-2 parameters so that it emulates MPEG-1 */
  1839. s->progressive_sequence = 1;
  1840. s->progressive_frame = 1;
  1841. s->picture_structure = PICT_FRAME;
  1842. s->frame_pred_frame_dct = 1;
  1843. s->chroma_format = 1;
  1844. s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG1VIDEO;
  1845. avctx->sub_id = 1; /* indicates MPEG-1 */
  1846. s->out_format = FMT_MPEG1;
  1847. s->swap_uv = 0;//AFAIK VCR2 does not have SEQ_HEADER
  1848. if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
  1849. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1850. av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
  1851. s->avctx->rc_buffer_size, s->bit_rate);
  1852. return 0;
  1853. }
  1854. static int vcr2_init_sequence(AVCodecContext *avctx)
  1855. {
  1856. Mpeg1Context *s1 = avctx->priv_data;
  1857. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1858. int i, v;
  1859. /* start new MPEG-1 context decoding */
  1860. s->out_format = FMT_MPEG1;
  1861. if (s1->mpeg_enc_ctx_allocated) {
  1862. MPV_common_end(s);
  1863. }
  1864. s->width = avctx->coded_width;
  1865. s->height = avctx->coded_height;
  1866. avctx->has_b_frames= 0; //true?
  1867. s->low_delay= 1;
  1868. avctx->pix_fmt = mpeg_get_pixelformat(avctx);
  1869. avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
  1870. if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel )
  1871. if( avctx->idct_algo == FF_IDCT_AUTO )
  1872. avctx->idct_algo = FF_IDCT_SIMPLE;
  1873. if (MPV_common_init(s) < 0)
  1874. return -1;
  1875. exchange_uv(s);//common init reset pblocks, so we swap them here
  1876. s->swap_uv = 1;// in case of xvmc we need to swap uv for each MB
  1877. s1->mpeg_enc_ctx_allocated = 1;
  1878. for(i=0;i<64;i++) {
  1879. int j= s->dsp.idct_permutation[i];
  1880. v = ff_mpeg1_default_intra_matrix[i];
  1881. s->intra_matrix[j] = v;
  1882. s->chroma_intra_matrix[j] = v;
  1883. v = ff_mpeg1_default_non_intra_matrix[i];
  1884. s->inter_matrix[j] = v;
  1885. s->chroma_inter_matrix[j] = v;
  1886. }
  1887. s->progressive_sequence = 1;
  1888. s->progressive_frame = 1;
  1889. s->picture_structure = PICT_FRAME;
  1890. s->frame_pred_frame_dct = 1;
  1891. s->chroma_format = 1;
  1892. s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
  1893. avctx->sub_id = 2; /* indicates MPEG-2 */
  1894. s1->save_width = s->width;
  1895. s1->save_height = s->height;
  1896. s1->save_progressive_seq = s->progressive_sequence;
  1897. return 0;
  1898. }
  1899. static void mpeg_decode_user_data(AVCodecContext *avctx,
  1900. const uint8_t *p, int buf_size)
  1901. {
  1902. const uint8_t *buf_end = p+buf_size;
  1903. /* we parse the DTG active format information */
  1904. if (buf_end - p >= 5 &&
  1905. p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
  1906. int flags = p[4];
  1907. p += 5;
  1908. if (flags & 0x80) {
  1909. /* skip event id */
  1910. p += 2;
  1911. }
  1912. if (flags & 0x40) {
  1913. if (buf_end - p < 1)
  1914. return;
  1915. avctx->dtg_active_format = p[0] & 0x0f;
  1916. }
  1917. }
  1918. }
  1919. static void mpeg_decode_gop(AVCodecContext *avctx,
  1920. const uint8_t *buf, int buf_size){
  1921. Mpeg1Context *s1 = avctx->priv_data;
  1922. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1923. int time_code_hours, time_code_minutes;
  1924. int time_code_seconds, time_code_pictures;
  1925. int broken_link;
  1926. init_get_bits(&s->gb, buf, buf_size*8);
  1927. skip_bits1(&s->gb); /* drop_frame_flag */
  1928. time_code_hours=get_bits(&s->gb,5);
  1929. time_code_minutes = get_bits(&s->gb,6);
  1930. skip_bits1(&s->gb);//marker bit
  1931. time_code_seconds = get_bits(&s->gb,6);
  1932. time_code_pictures = get_bits(&s->gb,6);
  1933. s->closed_gop = get_bits1(&s->gb);
  1934. /*broken_link indicate that after editing the
  1935. reference frames of the first B-Frames after GOP I-Frame
  1936. are missing (open gop)*/
  1937. broken_link = get_bits1(&s->gb);
  1938. if(s->avctx->debug & FF_DEBUG_PICT_INFO)
  1939. av_log(s->avctx, AV_LOG_DEBUG, "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
  1940. time_code_hours, time_code_minutes, time_code_seconds,
  1941. time_code_pictures, s->closed_gop, broken_link);
  1942. }
  1943. /**
  1944. * Find the end of the current frame in the bitstream.
  1945. * @return the position of the first byte of the next frame, or -1
  1946. */
  1947. int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size, AVCodecParserContext *s)
  1948. {
  1949. int i;
  1950. uint32_t state= pc->state;
  1951. /* EOF considered as end of frame */
  1952. if (buf_size == 0)
  1953. return 0;
  1954. /*
  1955. 0 frame start -> 1/4
  1956. 1 first_SEQEXT -> 0/2
  1957. 2 first field start -> 3/0
  1958. 3 second_SEQEXT -> 2/0
  1959. 4 searching end
  1960. */
  1961. for(i=0; i<buf_size; i++){
  1962. assert(pc->frame_start_found>=0 && pc->frame_start_found<=4);
  1963. if(pc->frame_start_found&1){
  1964. if(state == EXT_START_CODE && (buf[i]&0xF0) != 0x80)
  1965. pc->frame_start_found--;
  1966. else if(state == EXT_START_CODE+2){
  1967. if((buf[i]&3) == 3) pc->frame_start_found= 0;
  1968. else pc->frame_start_found= (pc->frame_start_found+1)&3;
  1969. }
  1970. state++;
  1971. }else{
  1972. i= ff_find_start_code(buf+i, buf+buf_size, &state) - buf - 1;
  1973. if(pc->frame_start_found==0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE){
  1974. i++;
  1975. pc->frame_start_found=4;
  1976. }
  1977. if(state == SEQ_END_CODE){
  1978. pc->state=-1;
  1979. return i+1;
  1980. }
  1981. if(pc->frame_start_found==2 && state == SEQ_START_CODE)
  1982. pc->frame_start_found= 0;
  1983. if(pc->frame_start_found<4 && state == EXT_START_CODE)
  1984. pc->frame_start_found++;
  1985. if(pc->frame_start_found == 4 && (state&0xFFFFFF00) == 0x100){
  1986. if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
  1987. pc->frame_start_found=0;
  1988. pc->state=-1;
  1989. return i-3;
  1990. }
  1991. }
  1992. if(pc->frame_start_found == 0 && s && state == PICTURE_START_CODE){
  1993. ff_fetch_timestamp(s, i-3, 1);
  1994. }
  1995. }
  1996. }
  1997. pc->state= state;
  1998. return END_NOT_FOUND;
  1999. }
  2000. static int decode_chunks(AVCodecContext *avctx,
  2001. AVFrame *picture, int *data_size,
  2002. const uint8_t *buf, int buf_size);
  2003. /* handle buffering and image synchronisation */
  2004. static int mpeg_decode_frame(AVCodecContext *avctx,
  2005. void *data, int *data_size,
  2006. AVPacket *avpkt)
  2007. {
  2008. const uint8_t *buf = avpkt->data;
  2009. int buf_size = avpkt->size;
  2010. Mpeg1Context *s = avctx->priv_data;
  2011. AVFrame *picture = data;
  2012. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  2013. av_dlog(avctx, "fill_buffer\n");
  2014. if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
  2015. /* special case for last picture */
  2016. if (s2->low_delay==0 && s2->next_picture_ptr) {
  2017. *picture= *(AVFrame*)s2->next_picture_ptr;
  2018. s2->next_picture_ptr= NULL;
  2019. *data_size = sizeof(AVFrame);
  2020. }
  2021. return buf_size;
  2022. }
  2023. if(s2->flags&CODEC_FLAG_TRUNCATED){
  2024. int next= ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size, NULL);
  2025. if( ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0 )
  2026. return buf_size;
  2027. }
  2028. #if 0
  2029. if (s->repeat_field % 2 == 1) {
  2030. s->repeat_field++;
  2031. //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
  2032. // s2->picture_number, s->repeat_field);
  2033. if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
  2034. *data_size = sizeof(AVPicture);
  2035. goto the_end;
  2036. }
  2037. }
  2038. #endif
  2039. if(s->mpeg_enc_ctx_allocated==0 && avctx->codec_tag == AV_RL32("VCR2"))
  2040. vcr2_init_sequence(avctx);
  2041. s->slice_count= 0;
  2042. if(avctx->extradata && !avctx->frame_number)
  2043. decode_chunks(avctx, picture, data_size, avctx->extradata, avctx->extradata_size);
  2044. return decode_chunks(avctx, picture, data_size, buf, buf_size);
  2045. }
  2046. static int decode_chunks(AVCodecContext *avctx,
  2047. AVFrame *picture, int *data_size,
  2048. const uint8_t *buf, int buf_size)
  2049. {
  2050. Mpeg1Context *s = avctx->priv_data;
  2051. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  2052. const uint8_t *buf_ptr = buf;
  2053. const uint8_t *buf_end = buf + buf_size;
  2054. int ret, input_size;
  2055. int last_code= 0;
  2056. for(;;) {
  2057. /* find next start code */
  2058. uint32_t start_code = -1;
  2059. buf_ptr = ff_find_start_code(buf_ptr,buf_end, &start_code);
  2060. if (start_code > 0x1ff){
  2061. if(s2->pict_type != AV_PICTURE_TYPE_B || avctx->skip_frame <= AVDISCARD_DEFAULT){
  2062. if(HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)){
  2063. int i;
  2064. assert(avctx->thread_count > 1);
  2065. avctx->execute(avctx, slice_decode_thread, &s2->thread_context[0], NULL, s->slice_count, sizeof(void*));
  2066. for(i=0; i<s->slice_count; i++)
  2067. s2->error_count += s2->thread_context[i]->error_count;
  2068. }
  2069. if (CONFIG_VDPAU && uses_vdpau(avctx))
  2070. ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);
  2071. if (slice_end(avctx, picture)) {
  2072. if(s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
  2073. *data_size = sizeof(AVPicture);
  2074. }
  2075. }
  2076. s2->pict_type= 0;
  2077. return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
  2078. }
  2079. input_size = buf_end - buf_ptr;
  2080. if(avctx->debug & FF_DEBUG_STARTCODE){
  2081. av_log(avctx, AV_LOG_DEBUG, "%3X at %td left %d\n", start_code, buf_ptr-buf, input_size);
  2082. }
  2083. /* prepare data for next start code */
  2084. switch(start_code) {
  2085. case SEQ_START_CODE:
  2086. if(last_code == 0){
  2087. mpeg1_decode_sequence(avctx, buf_ptr,
  2088. input_size);
  2089. s->sync=1;
  2090. }else{
  2091. av_log(avctx, AV_LOG_ERROR, "ignoring SEQ_START_CODE after %X\n", last_code);
  2092. }
  2093. break;
  2094. case PICTURE_START_CODE:
  2095. if (HAVE_THREADS && (avctx->active_thread_type&FF_THREAD_SLICE) && s->slice_count) {
  2096. int i;
  2097. avctx->execute(avctx, slice_decode_thread,
  2098. s2->thread_context, NULL,
  2099. s->slice_count, sizeof(void*));
  2100. for (i = 0; i < s->slice_count; i++)
  2101. s2->error_count += s2->thread_context[i]->error_count;
  2102. s->slice_count = 0;
  2103. }
  2104. if(last_code == 0 || last_code == SLICE_MIN_START_CODE){
  2105. if(mpeg_decode_postinit(avctx) < 0){
  2106. av_log(avctx, AV_LOG_ERROR, "mpeg_decode_postinit() failure\n");
  2107. return -1;
  2108. }
  2109. /* we have a complete image: we try to decompress it */
  2110. if(mpeg1_decode_picture(avctx,
  2111. buf_ptr, input_size) < 0)
  2112. s2->pict_type=0;
  2113. s2->first_slice = 1;
  2114. last_code= PICTURE_START_CODE;
  2115. }else{
  2116. av_log(avctx, AV_LOG_ERROR, "ignoring pic after %X\n", last_code);
  2117. }
  2118. break;
  2119. case EXT_START_CODE:
  2120. init_get_bits(&s2->gb, buf_ptr, input_size*8);
  2121. switch(get_bits(&s2->gb, 4)) {
  2122. case 0x1:
  2123. if(last_code == 0){
  2124. mpeg_decode_sequence_extension(s);
  2125. }else{
  2126. av_log(avctx, AV_LOG_ERROR, "ignoring seq ext after %X\n", last_code);
  2127. }
  2128. break;
  2129. case 0x2:
  2130. mpeg_decode_sequence_display_extension(s);
  2131. break;
  2132. case 0x3:
  2133. mpeg_decode_quant_matrix_extension(s2);
  2134. break;
  2135. case 0x7:
  2136. mpeg_decode_picture_display_extension(s);
  2137. break;
  2138. case 0x8:
  2139. if(last_code == PICTURE_START_CODE){
  2140. mpeg_decode_picture_coding_extension(s);
  2141. }else{
  2142. av_log(avctx, AV_LOG_ERROR, "ignoring pic cod ext after %X\n", last_code);
  2143. }
  2144. break;
  2145. }
  2146. break;
  2147. case USER_START_CODE:
  2148. mpeg_decode_user_data(avctx,
  2149. buf_ptr, input_size);
  2150. break;
  2151. case GOP_START_CODE:
  2152. if(last_code == 0){
  2153. s2->first_field=0;
  2154. mpeg_decode_gop(avctx,
  2155. buf_ptr, input_size);
  2156. s->sync=1;
  2157. }else{
  2158. av_log(avctx, AV_LOG_ERROR, "ignoring GOP_START_CODE after %X\n", last_code);
  2159. }
  2160. break;
  2161. default:
  2162. if (start_code >= SLICE_MIN_START_CODE &&
  2163. start_code <= SLICE_MAX_START_CODE && last_code!=0) {
  2164. const int field_pic= s2->picture_structure != PICT_FRAME;
  2165. int mb_y= (start_code - SLICE_MIN_START_CODE) << field_pic;
  2166. last_code= SLICE_MIN_START_CODE;
  2167. if(s2->picture_structure == PICT_BOTTOM_FIELD)
  2168. mb_y++;
  2169. if (mb_y >= s2->mb_height){
  2170. av_log(s2->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
  2171. return -1;
  2172. }
  2173. if(s2->last_picture_ptr==NULL){
  2174. /* Skip B-frames if we do not have reference frames and gop is not closed */
  2175. if(s2->pict_type==AV_PICTURE_TYPE_B){
  2176. if(!s2->closed_gop)
  2177. break;
  2178. }
  2179. }
  2180. if(s2->pict_type==AV_PICTURE_TYPE_I)
  2181. s->sync=1;
  2182. if(s2->next_picture_ptr==NULL){
  2183. /* Skip P-frames if we do not have a reference frame or we have an invalid header. */
  2184. if(s2->pict_type==AV_PICTURE_TYPE_P && !s->sync) break;
  2185. }
  2186. if( (avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type==AV_PICTURE_TYPE_B)
  2187. ||(avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type!=AV_PICTURE_TYPE_I)
  2188. || avctx->skip_frame >= AVDISCARD_ALL)
  2189. break;
  2190. if (!s->mpeg_enc_ctx_allocated) break;
  2191. if(s2->codec_id == CODEC_ID_MPEG2VIDEO){
  2192. if(mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
  2193. break;
  2194. }
  2195. if(!s2->pict_type){
  2196. av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
  2197. break;
  2198. }
  2199. if(s2->first_slice){
  2200. s2->first_slice=0;
  2201. if(mpeg_field_start(s2, buf, buf_size) < 0)
  2202. return -1;
  2203. }
  2204. if(!s2->current_picture_ptr){
  2205. av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\n");
  2206. return -1;
  2207. }
  2208. if (uses_vdpau(avctx)) {
  2209. s->slice_count++;
  2210. break;
  2211. }
  2212. if(HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)){
  2213. int threshold= (s2->mb_height*s->slice_count + avctx->thread_count/2) / avctx->thread_count;
  2214. assert(avctx->thread_count > 1);
  2215. if(threshold <= mb_y){
  2216. MpegEncContext *thread_context= s2->thread_context[s->slice_count];
  2217. thread_context->start_mb_y= mb_y;
  2218. thread_context->end_mb_y = s2->mb_height;
  2219. if(s->slice_count){
  2220. s2->thread_context[s->slice_count-1]->end_mb_y= mb_y;
  2221. ff_update_duplicate_context(thread_context, s2);
  2222. }
  2223. init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
  2224. s->slice_count++;
  2225. }
  2226. buf_ptr += 2; //FIXME add minimum number of bytes per slice
  2227. }else{
  2228. ret = mpeg_decode_slice(s, mb_y, &buf_ptr, input_size);
  2229. emms_c();
  2230. if(ret < 0){
  2231. if(s2->resync_mb_x>=0 && s2->resync_mb_y>=0)
  2232. 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);
  2233. }else{
  2234. 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);
  2235. }
  2236. }
  2237. }
  2238. break;
  2239. }
  2240. }
  2241. }
  2242. static void flush(AVCodecContext *avctx){
  2243. Mpeg1Context *s = avctx->priv_data;
  2244. s->sync=0;
  2245. ff_mpeg_flush(avctx);
  2246. }
  2247. static int mpeg_decode_end(AVCodecContext *avctx)
  2248. {
  2249. Mpeg1Context *s = avctx->priv_data;
  2250. if (s->mpeg_enc_ctx_allocated)
  2251. MPV_common_end(&s->mpeg_enc_ctx);
  2252. return 0;
  2253. }
  2254. static const AVProfile mpeg2_video_profiles[] = {
  2255. { FF_PROFILE_MPEG2_422, "4:2:2" },
  2256. { FF_PROFILE_MPEG2_HIGH, "High" },
  2257. { FF_PROFILE_MPEG2_SS, "Spatially Scalable" },
  2258. { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable" },
  2259. { FF_PROFILE_MPEG2_MAIN, "Main" },
  2260. { FF_PROFILE_MPEG2_SIMPLE, "Simple" },
  2261. { FF_PROFILE_RESERVED, "Reserved" },
  2262. { FF_PROFILE_RESERVED, "Reserved" },
  2263. { FF_PROFILE_UNKNOWN },
  2264. };
  2265. AVCodec ff_mpeg1video_decoder = {
  2266. "mpeg1video",
  2267. AVMEDIA_TYPE_VIDEO,
  2268. CODEC_ID_MPEG1VIDEO,
  2269. sizeof(Mpeg1Context),
  2270. mpeg_decode_init,
  2271. NULL,
  2272. mpeg_decode_end,
  2273. mpeg_decode_frame,
  2274. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  2275. .flush= flush,
  2276. .max_lowres= 3,
  2277. .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
  2278. .update_thread_context= ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
  2279. };
  2280. AVCodec ff_mpeg2video_decoder = {
  2281. "mpeg2video",
  2282. AVMEDIA_TYPE_VIDEO,
  2283. CODEC_ID_MPEG2VIDEO,
  2284. sizeof(Mpeg1Context),
  2285. mpeg_decode_init,
  2286. NULL,
  2287. mpeg_decode_end,
  2288. mpeg_decode_frame,
  2289. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
  2290. .flush= flush,
  2291. .max_lowres= 3,
  2292. .long_name= NULL_IF_CONFIG_SMALL("MPEG-2 video"),
  2293. .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
  2294. };
  2295. //legacy decoder
  2296. AVCodec ff_mpegvideo_decoder = {
  2297. "mpegvideo",
  2298. AVMEDIA_TYPE_VIDEO,
  2299. CODEC_ID_MPEG2VIDEO,
  2300. sizeof(Mpeg1Context),
  2301. mpeg_decode_init,
  2302. NULL,
  2303. mpeg_decode_end,
  2304. mpeg_decode_frame,
  2305. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
  2306. .flush= flush,
  2307. .max_lowres= 3,
  2308. .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
  2309. };
  2310. #if CONFIG_MPEG_XVMC_DECODER
  2311. static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx){
  2312. if( avctx->active_thread_type & FF_THREAD_SLICE )
  2313. return -1;
  2314. if( !(avctx->slice_flags & SLICE_FLAG_CODED_ORDER) )
  2315. return -1;
  2316. if( !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD) ){
  2317. av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
  2318. }
  2319. mpeg_decode_init(avctx);
  2320. avctx->pix_fmt = PIX_FMT_XVMC_MPEG2_IDCT;
  2321. avctx->xvmc_acceleration = 2;//2 - the blocks are packed!
  2322. return 0;
  2323. }
  2324. AVCodec ff_mpeg_xvmc_decoder = {
  2325. "mpegvideo_xvmc",
  2326. AVMEDIA_TYPE_VIDEO,
  2327. CODEC_ID_MPEG2VIDEO_XVMC,
  2328. sizeof(Mpeg1Context),
  2329. mpeg_mc_decode_init,
  2330. NULL,
  2331. mpeg_decode_end,
  2332. mpeg_decode_frame,
  2333. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
  2334. .flush= flush,
  2335. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
  2336. };
  2337. #endif
  2338. #if CONFIG_MPEG_VDPAU_DECODER
  2339. AVCodec ff_mpeg_vdpau_decoder = {
  2340. "mpegvideo_vdpau",
  2341. AVMEDIA_TYPE_VIDEO,
  2342. CODEC_ID_MPEG2VIDEO,
  2343. sizeof(Mpeg1Context),
  2344. mpeg_decode_init,
  2345. NULL,
  2346. mpeg_decode_end,
  2347. mpeg_decode_frame,
  2348. CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
  2349. .flush= flush,
  2350. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
  2351. };
  2352. #endif
  2353. #if CONFIG_MPEG1_VDPAU_DECODER
  2354. AVCodec ff_mpeg1_vdpau_decoder = {
  2355. "mpeg1video_vdpau",
  2356. AVMEDIA_TYPE_VIDEO,
  2357. CODEC_ID_MPEG1VIDEO,
  2358. sizeof(Mpeg1Context),
  2359. mpeg_decode_init,
  2360. NULL,
  2361. mpeg_decode_end,
  2362. mpeg_decode_frame,
  2363. CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
  2364. .flush= flush,
  2365. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),
  2366. };
  2367. #endif