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.

2672 lines
92KB

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