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.

839 lines
25KB

  1. /*
  2. * H.263 decoder
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * @file h263dec.c
  21. * H.263 decoder.
  22. */
  23. #include "avcodec.h"
  24. #include "dsputil.h"
  25. #include "mpegvideo.h"
  26. //#define DEBUG
  27. //#define PRINT_FRAME_TIME
  28. int ff_h263_decode_init(AVCodecContext *avctx)
  29. {
  30. MpegEncContext *s = avctx->priv_data;
  31. s->avctx = avctx;
  32. s->out_format = FMT_H263;
  33. s->width = avctx->width;
  34. s->height = avctx->height;
  35. s->workaround_bugs= avctx->workaround_bugs;
  36. // set defaults
  37. s->quant_precision=5;
  38. s->progressive_sequence=1;
  39. s->decode_mb= ff_h263_decode_mb;
  40. s->low_delay= 1;
  41. avctx->pix_fmt= PIX_FMT_YUV420P;
  42. s->unrestricted_mv= 1;
  43. /* select sub codec */
  44. switch(avctx->codec->id) {
  45. case CODEC_ID_H263:
  46. s->gob_number = 0;
  47. s->unrestricted_mv= 0;
  48. break;
  49. case CODEC_ID_MPEG4:
  50. s->time_increment_bits = 4; /* default value for broken headers */
  51. s->h263_pred = 1;
  52. s->low_delay = 0; //default, might be overriden in the vol header during header parsing
  53. break;
  54. case CODEC_ID_MSMPEG4V1:
  55. s->h263_msmpeg4 = 1;
  56. s->h263_pred = 1;
  57. s->msmpeg4_version=1;
  58. break;
  59. case CODEC_ID_MSMPEG4V2:
  60. s->h263_msmpeg4 = 1;
  61. s->h263_pred = 1;
  62. s->msmpeg4_version=2;
  63. break;
  64. case CODEC_ID_MSMPEG4V3:
  65. s->h263_msmpeg4 = 1;
  66. s->h263_pred = 1;
  67. s->msmpeg4_version=3;
  68. break;
  69. case CODEC_ID_WMV1:
  70. s->h263_msmpeg4 = 1;
  71. s->h263_pred = 1;
  72. s->msmpeg4_version=4;
  73. break;
  74. case CODEC_ID_WMV2:
  75. s->h263_msmpeg4 = 1;
  76. s->h263_pred = 1;
  77. s->msmpeg4_version=5;
  78. break;
  79. case CODEC_ID_H263I:
  80. s->h263_intel = 1;
  81. break;
  82. case CODEC_ID_FLV1:
  83. s->h263_flv = 1;
  84. break;
  85. default:
  86. return -1;
  87. }
  88. s->codec_id= avctx->codec->id;
  89. /* for h263, we allocate the images after having read the header */
  90. if (avctx->codec->id != CODEC_ID_H263 && avctx->codec->id != CODEC_ID_MPEG4)
  91. if (MPV_common_init(s) < 0)
  92. return -1;
  93. if (s->h263_msmpeg4)
  94. ff_msmpeg4_decode_init(s);
  95. else
  96. h263_decode_init_vlc(s);
  97. return 0;
  98. }
  99. int ff_h263_decode_end(AVCodecContext *avctx)
  100. {
  101. MpegEncContext *s = avctx->priv_data;
  102. MPV_common_end(s);
  103. return 0;
  104. }
  105. /**
  106. * retunrs the number of bytes consumed for building the current frame
  107. */
  108. static int get_consumed_bytes(MpegEncContext *s, int buf_size){
  109. int pos= (get_bits_count(&s->gb)+7)>>3;
  110. if(s->divx_packed){
  111. //we would have to scan through the whole buf to handle the weird reordering ...
  112. return buf_size;
  113. }else if(s->flags&CODEC_FLAG_TRUNCATED){
  114. pos -= s->parse_context.last_index;
  115. if(pos<0) pos=0; // padding is not really read so this might be -1
  116. return pos;
  117. }else{
  118. if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
  119. if(pos+10>buf_size) pos=buf_size; // oops ;)
  120. return pos;
  121. }
  122. }
  123. static int decode_slice(MpegEncContext *s){
  124. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  125. s->last_resync_gb= s->gb;
  126. s->first_slice_line= 1;
  127. s->resync_mb_x= s->mb_x;
  128. s->resync_mb_y= s->mb_y;
  129. s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
  130. s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
  131. if(s->partitioned_frame){
  132. const int qscale= s->qscale;
  133. if(s->codec_id==CODEC_ID_MPEG4){
  134. if(ff_mpeg4_decode_partitions(s) < 0)
  135. return -1;
  136. }
  137. /* restore variables which were modified */
  138. s->first_slice_line=1;
  139. s->mb_x= s->resync_mb_x;
  140. s->mb_y= s->resync_mb_y;
  141. s->qscale= qscale;
  142. s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
  143. s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
  144. }
  145. for(; s->mb_y < s->mb_height; s->mb_y++) {
  146. /* per-row end of slice checks */
  147. if(s->msmpeg4_version){
  148. if(s->resync_mb_y + s->slice_height == s->mb_y){
  149. 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);
  150. return 0;
  151. }
  152. }
  153. if(s->msmpeg4_version==1){
  154. s->last_dc[0]=
  155. s->last_dc[1]=
  156. s->last_dc[2]= 128;
  157. }
  158. ff_init_block_index(s);
  159. for(; s->mb_x < s->mb_width; s->mb_x++) {
  160. int ret;
  161. ff_update_block_index(s);
  162. if(s->resync_mb_x == s->mb_x && s->resync_mb_y+1 == s->mb_y){
  163. s->first_slice_line=0;
  164. }
  165. /* DCT & quantize */
  166. s->dsp.clear_blocks(s->block[0]);
  167. s->mv_dir = MV_DIR_FORWARD;
  168. s->mv_type = MV_TYPE_16X16;
  169. // s->mb_skiped = 0;
  170. //printf("%d %d %06X\n", ret, get_bits_count(&s->gb), show_bits(&s->gb, 24));
  171. ret= s->decode_mb(s, s->block);
  172. if (s->pict_type!=B_TYPE)
  173. ff_h263_update_motion_val(s);
  174. if(ret<0){
  175. const int xy= s->mb_x + s->mb_y*s->mb_stride;
  176. if(ret==SLICE_END){
  177. MPV_decode_mb(s, s->block);
  178. if(s->loop_filter)
  179. ff_h263_loop_filter(s);
  180. //printf("%d %d %d %06X\n", s->mb_x, s->mb_y, s->gb.size*8 - get_bits_count(&s->gb), show_bits(&s->gb, 24));
  181. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
  182. s->padding_bug_score--;
  183. if(++s->mb_x >= s->mb_width){
  184. s->mb_x=0;
  185. ff_draw_horiz_band(s, s->mb_y*16, 16);
  186. s->mb_y++;
  187. }
  188. return 0;
  189. }else if(ret==SLICE_NOEND){
  190. av_log(s->avctx, AV_LOG_ERROR, "Slice mismatch at MB: %d\n", xy);
  191. 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)&part_mask);
  192. return -1;
  193. }
  194. av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", xy);
  195. 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)&part_mask);
  196. return -1;
  197. }
  198. MPV_decode_mb(s, s->block);
  199. if(s->loop_filter)
  200. ff_h263_loop_filter(s);
  201. }
  202. ff_draw_horiz_band(s, s->mb_y*16, 16);
  203. s->mb_x= 0;
  204. }
  205. assert(s->mb_x==0 && s->mb_y==s->mb_height);
  206. /* try to detect the padding bug */
  207. if( s->codec_id==CODEC_ID_MPEG4
  208. && (s->workaround_bugs&FF_BUG_AUTODETECT)
  209. && s->gb.size_in_bits - get_bits_count(&s->gb) >=0
  210. && s->gb.size_in_bits - get_bits_count(&s->gb) < 48
  211. // && !s->resync_marker
  212. && !s->data_partitioning){
  213. const int bits_count= get_bits_count(&s->gb);
  214. const int bits_left = s->gb.size_in_bits - bits_count;
  215. if(bits_left==0){
  216. s->padding_bug_score+=16;
  217. }else if(bits_left>8){
  218. s->padding_bug_score++;
  219. } else if(bits_left != 1){
  220. int v= show_bits(&s->gb, 8);
  221. v|= 0x7F >> (7-(bits_count&7));
  222. if(v==0x7F)
  223. s->padding_bug_score--;
  224. else
  225. s->padding_bug_score++;
  226. }
  227. }
  228. // handle formats which dont have unique end markers
  229. if(s->msmpeg4_version || (s->workaround_bugs&FF_BUG_NO_PADDING)){ //FIXME perhaps solve this more cleanly
  230. int left= s->gb.size_in_bits - get_bits_count(&s->gb);
  231. int max_extra=7;
  232. /* no markers in M$ crap */
  233. if(s->msmpeg4_version && s->pict_type==I_TYPE)
  234. max_extra+= 17;
  235. /* buggy padding but the frame should still end approximately at the bitstream end */
  236. if((s->workaround_bugs&FF_BUG_NO_PADDING) && s->error_resilience>=3)
  237. max_extra+= 48;
  238. else if((s->workaround_bugs&FF_BUG_NO_PADDING))
  239. max_extra+= 256*256*256*64;
  240. if(left>max_extra){
  241. av_log(s->avctx, AV_LOG_ERROR, "discarding %d junk bits at end, next would be %X\n", left, show_bits(&s->gb, 24));
  242. }
  243. else if(left<0){
  244. av_log(s->avctx, AV_LOG_ERROR, "overreading %d bits\n", -left);
  245. }else
  246. 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);
  247. return 0;
  248. }
  249. av_log(s->avctx, AV_LOG_ERROR, "slice end not reached but screenspace end (%d left %06X, score= %d)\n",
  250. s->gb.size_in_bits - get_bits_count(&s->gb),
  251. show_bits(&s->gb, 24), s->padding_bug_score);
  252. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
  253. return -1;
  254. }
  255. /**
  256. * finds the end of the current frame in the bitstream.
  257. * @return the position of the first byte of the next frame, or -1
  258. */
  259. static int mpeg4_find_frame_end(MpegEncContext *s, uint8_t *buf, int buf_size){
  260. ParseContext *pc= &s->parse_context;
  261. int vop_found, i;
  262. uint32_t state;
  263. vop_found= pc->frame_start_found;
  264. state= pc->state;
  265. i=0;
  266. if(!vop_found){
  267. for(i=0; i<buf_size; i++){
  268. state= (state<<8) | buf[i];
  269. if(state == 0x1B6){
  270. i++;
  271. vop_found=1;
  272. break;
  273. }
  274. }
  275. }
  276. if(vop_found){
  277. for(; i<buf_size; i++){
  278. state= (state<<8) | buf[i];
  279. if((state&0xFFFFFF00) == 0x100){
  280. pc->frame_start_found=0;
  281. pc->state=-1;
  282. return i-3;
  283. }
  284. }
  285. }
  286. pc->frame_start_found= vop_found;
  287. pc->state= state;
  288. return END_NOT_FOUND;
  289. }
  290. static int h263_find_frame_end(MpegEncContext *s, uint8_t *buf, int buf_size){
  291. ParseContext *pc= &s->parse_context;
  292. int vop_found, i;
  293. uint32_t state;
  294. vop_found= pc->frame_start_found;
  295. state= pc->state;
  296. i=0;
  297. if(!vop_found){
  298. for(i=0; i<buf_size; i++){
  299. state= (state<<8) | buf[i];
  300. if(state>>(32-22) == 0x20){
  301. i++;
  302. vop_found=1;
  303. break;
  304. }
  305. }
  306. }
  307. if(vop_found){
  308. for(; i<buf_size; i++){
  309. state= (state<<8) | buf[i];
  310. if(state>>(32-22) == 0x20){
  311. pc->frame_start_found=0;
  312. pc->state=-1;
  313. return i-3;
  314. }
  315. }
  316. }
  317. pc->frame_start_found= vop_found;
  318. pc->state= state;
  319. return END_NOT_FOUND;
  320. }
  321. int ff_h263_decode_frame(AVCodecContext *avctx,
  322. void *data, int *data_size,
  323. uint8_t *buf, int buf_size)
  324. {
  325. MpegEncContext *s = avctx->priv_data;
  326. int ret;
  327. AVFrame *pict = data;
  328. #ifdef PRINT_FRAME_TIME
  329. uint64_t time= rdtsc();
  330. #endif
  331. #ifdef DEBUG
  332. printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
  333. printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
  334. #endif
  335. s->flags= avctx->flags;
  336. *data_size = 0;
  337. /* no supplementary picture */
  338. if (buf_size == 0) {
  339. /* special case for last picture */
  340. if (s->low_delay==0 && s->next_picture_ptr) {
  341. *pict= *(AVFrame*)s->next_picture_ptr;
  342. s->next_picture_ptr= NULL;
  343. *data_size = sizeof(AVFrame);
  344. }
  345. return 0;
  346. }
  347. if(s->flags&CODEC_FLAG_TRUNCATED){
  348. int next;
  349. if(s->codec_id==CODEC_ID_MPEG4){
  350. next= mpeg4_find_frame_end(s, buf, buf_size);
  351. }else if(s->codec_id==CODEC_ID_H263){
  352. next= h263_find_frame_end(s, buf, buf_size);
  353. }else{
  354. av_log(s->avctx, AV_LOG_ERROR, "this codec doesnt support truncated bitstreams\n");
  355. return -1;
  356. }
  357. if( ff_combine_frame(s, next, &buf, &buf_size) < 0 )
  358. return buf_size;
  359. }
  360. retry:
  361. if(s->bitstream_buffer_size && buf_size<20){ //divx 5.01+ frame reorder
  362. init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size*8);
  363. }else
  364. init_get_bits(&s->gb, buf, buf_size*8);
  365. s->bitstream_buffer_size=0;
  366. if (!s->context_initialized) {
  367. if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
  368. return -1;
  369. }
  370. //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
  371. if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
  372. int i= ff_find_unused_picture(s, 0);
  373. s->current_picture_ptr= &s->picture[i];
  374. }
  375. /* let's go :-) */
  376. if (s->msmpeg4_version==5) {
  377. ret= ff_wmv2_decode_picture_header(s);
  378. } else if (s->msmpeg4_version) {
  379. ret = msmpeg4_decode_picture_header(s);
  380. } else if (s->h263_pred) {
  381. if(s->avctx->extradata_size && s->picture_number==0){
  382. GetBitContext gb;
  383. init_get_bits(&gb, s->avctx->extradata, s->avctx->extradata_size*8);
  384. ret = ff_mpeg4_decode_picture_header(s, &gb);
  385. }
  386. ret = ff_mpeg4_decode_picture_header(s, &s->gb);
  387. if(s->flags& CODEC_FLAG_LOW_DELAY)
  388. s->low_delay=1;
  389. } else if (s->h263_intel) {
  390. ret = intel_h263_decode_picture_header(s);
  391. } else if (s->h263_flv) {
  392. ret = flv_h263_decode_picture_header(s);
  393. } else {
  394. ret = h263_decode_picture_header(s);
  395. }
  396. if(ret==FRAME_SKIPED) return get_consumed_bytes(s, buf_size);
  397. /* skip if the header was thrashed */
  398. if (ret < 0){
  399. av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
  400. return -1;
  401. }
  402. avctx->has_b_frames= !s->low_delay;
  403. if(s->xvid_build==0 && s->divx_version==0 && s->lavc_build==0){
  404. if(s->avctx->stream_codec_tag == ff_get_fourcc("XVID") ||
  405. s->avctx->codec_tag == ff_get_fourcc("XVID") || s->avctx->codec_tag == ff_get_fourcc("XVIX"))
  406. s->xvid_build= -1;
  407. #if 0
  408. if(s->avctx->codec_tag == ff_get_fourcc("DIVX") && s->vo_type==0 && s->vol_control_parameters==1
  409. && s->padding_bug_score > 0 && s->low_delay) // XVID with modified fourcc
  410. s->xvid_build= -1;
  411. #endif
  412. }
  413. if(s->xvid_build==0 && s->divx_version==0 && s->lavc_build==0){
  414. if(s->avctx->codec_tag == ff_get_fourcc("DIVX") && s->vo_type==0 && s->vol_control_parameters==0)
  415. s->divx_version= 400; //divx 4
  416. }
  417. if(s->workaround_bugs&FF_BUG_AUTODETECT){
  418. s->workaround_bugs &= ~FF_BUG_NO_PADDING;
  419. if(s->padding_bug_score > -2 && !s->data_partitioning && (s->divx_version || !s->resync_marker))
  420. s->workaround_bugs |= FF_BUG_NO_PADDING;
  421. if(s->avctx->codec_tag == ff_get_fourcc("XVIX"))
  422. s->workaround_bugs|= FF_BUG_XVID_ILACE;
  423. if(s->avctx->codec_tag == ff_get_fourcc("UMP4")){
  424. s->workaround_bugs|= FF_BUG_UMP4;
  425. }
  426. if(s->divx_version>=500){
  427. s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
  428. }
  429. if(s->divx_version>502){
  430. s->workaround_bugs|= FF_BUG_QPEL_CHROMA2;
  431. }
  432. if(s->xvid_build && s->xvid_build<=3)
  433. s->padding_bug_score= 256*256*256*64;
  434. if(s->xvid_build && s->xvid_build<=1)
  435. s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
  436. if(s->xvid_build && s->xvid_build<=12)
  437. s->workaround_bugs|= FF_BUG_EDGE;
  438. #define SET_QPEL_FUNC(postfix1, postfix2) \
  439. s->dsp.put_ ## postfix1 = ff_put_ ## postfix2;\
  440. s->dsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2;\
  441. s->dsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
  442. if(s->lavc_build && s->lavc_build<4653)
  443. s->workaround_bugs|= FF_BUG_STD_QPEL;
  444. if(s->lavc_build && s->lavc_build<4655)
  445. s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
  446. if(s->lavc_build && s->lavc_build<4670){
  447. s->workaround_bugs|= FF_BUG_EDGE;
  448. }
  449. if(s->divx_version)
  450. s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
  451. //printf("padding_bug_score: %d\n", s->padding_bug_score);
  452. if(s->divx_version==501 && s->divx_build==20020416)
  453. s->padding_bug_score= 256*256*256*64;
  454. if(s->divx_version && s->divx_version<500){
  455. s->workaround_bugs|= FF_BUG_EDGE;
  456. }
  457. #if 0
  458. if(s->divx_version==500)
  459. s->padding_bug_score= 256*256*256*64;
  460. /* very ugly XVID padding bug detection FIXME/XXX solve this differently
  461. * lets hope this at least works
  462. */
  463. if( s->resync_marker==0 && s->data_partitioning==0 && s->divx_version==0
  464. && s->codec_id==CODEC_ID_MPEG4 && s->vo_type==0)
  465. s->workaround_bugs|= FF_BUG_NO_PADDING;
  466. if(s->lavc_build && s->lavc_build<4609) //FIXME not sure about the version num but a 4609 file seems ok
  467. s->workaround_bugs|= FF_BUG_NO_PADDING;
  468. #endif
  469. }
  470. if(s->workaround_bugs& FF_BUG_STD_QPEL){
  471. SET_QPEL_FUNC(qpel_pixels_tab[0][ 5], qpel16_mc11_old_c)
  472. SET_QPEL_FUNC(qpel_pixels_tab[0][ 7], qpel16_mc31_old_c)
  473. SET_QPEL_FUNC(qpel_pixels_tab[0][ 9], qpel16_mc12_old_c)
  474. SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
  475. SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
  476. SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
  477. SET_QPEL_FUNC(qpel_pixels_tab[1][ 5], qpel8_mc11_old_c)
  478. SET_QPEL_FUNC(qpel_pixels_tab[1][ 7], qpel8_mc31_old_c)
  479. SET_QPEL_FUNC(qpel_pixels_tab[1][ 9], qpel8_mc12_old_c)
  480. SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
  481. SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
  482. SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
  483. }
  484. if(avctx->debug & FF_DEBUG_BUGS)
  485. av_log(s->avctx, AV_LOG_DEBUG, "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
  486. s->workaround_bugs, s->lavc_build, s->xvid_build, s->divx_version, s->divx_build,
  487. s->divx_packed ? "p" : "");
  488. #if 0 // dump bits per frame / qp / complexity
  489. {
  490. static FILE *f=NULL;
  491. if(!f) f=fopen("rate_qp_cplx.txt", "w");
  492. fprintf(f, "%d %d %f\n", buf_size, s->qscale, buf_size*(double)s->qscale);
  493. }
  494. #endif
  495. /* After H263 & mpeg4 header decode we have the height, width,*/
  496. /* and other parameters. So then we could init the picture */
  497. /* FIXME: By the way H263 decoder is evolving it should have */
  498. /* an H263EncContext */
  499. if ( s->width != avctx->width || s->height != avctx->height) {
  500. /* H.263 could change picture size any time */
  501. ParseContext pc= s->parse_context; //FIXME move these demuxng hack to avformat
  502. s->parse_context.buffer=0;
  503. MPV_common_end(s);
  504. s->parse_context= pc;
  505. }
  506. if (!s->context_initialized) {
  507. avctx->width = s->width;
  508. avctx->height = s->height;
  509. goto retry;
  510. }
  511. if((s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P))
  512. s->gob_index = ff_h263_get_gob_height(s);
  513. // for hurry_up==5
  514. s->current_picture.pict_type= s->pict_type;
  515. s->current_picture.key_frame= s->pict_type == I_TYPE;
  516. /* skip b frames if we dont have reference frames */
  517. if(s->last_picture_ptr==NULL && s->pict_type==B_TYPE) return get_consumed_bytes(s, buf_size);
  518. /* skip b frames if we are in a hurry */
  519. if(avctx->hurry_up && s->pict_type==B_TYPE) return get_consumed_bytes(s, buf_size);
  520. /* skip everything if we are in a hurry>=5 */
  521. if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size);
  522. if(s->next_p_frame_damaged){
  523. if(s->pict_type==B_TYPE)
  524. return get_consumed_bytes(s, buf_size);
  525. else
  526. s->next_p_frame_damaged=0;
  527. }
  528. if(MPV_frame_start(s, avctx) < 0)
  529. return -1;
  530. #ifdef DEBUG
  531. printf("qscale=%d\n", s->qscale);
  532. #endif
  533. ff_er_frame_start(s);
  534. //the second part of the wmv2 header contains the MB skip bits which are stored in current_picture->mb_type
  535. //which isnt available before MPV_frame_start()
  536. if (s->msmpeg4_version==5){
  537. if(ff_wmv2_decode_secondary_picture_header(s) < 0)
  538. return -1;
  539. }
  540. /* decode each macroblock */
  541. s->mb_x=0;
  542. s->mb_y=0;
  543. decode_slice(s);
  544. while(s->mb_y<s->mb_height){
  545. if(s->msmpeg4_version){
  546. if(s->mb_x!=0 || (s->mb_y%s->slice_height)!=0 || get_bits_count(&s->gb) > s->gb.size_in_bits)
  547. break;
  548. }else{
  549. if(ff_h263_resync(s)<0)
  550. break;
  551. }
  552. if(s->msmpeg4_version<4 && s->h263_pred)
  553. ff_mpeg4_clean_buffers(s);
  554. decode_slice(s);
  555. }
  556. if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==I_TYPE)
  557. if(msmpeg4_decode_ext_header(s, buf_size) < 0){
  558. s->error_status_table[s->mb_num-1]= AC_ERROR|DC_ERROR|MV_ERROR;
  559. }
  560. /* divx 5.01+ bistream reorder stuff */
  561. if(s->codec_id==CODEC_ID_MPEG4 && s->bitstream_buffer_size==0 && s->divx_packed){
  562. int current_pos= get_bits_count(&s->gb)>>3;
  563. if( buf_size - current_pos > 5
  564. && buf_size - current_pos < BITSTREAM_BUFFER_SIZE){
  565. int i;
  566. int startcode_found=0;
  567. for(i=current_pos; i<buf_size-3; i++){
  568. if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==1 && buf[i+3]==0xB6){
  569. startcode_found=1;
  570. break;
  571. }
  572. }
  573. if(startcode_found){
  574. memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
  575. s->bitstream_buffer_size= buf_size - current_pos;
  576. }
  577. }
  578. }
  579. ff_er_frame_end(s);
  580. MPV_frame_end(s);
  581. assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
  582. assert(s->current_picture.pict_type == s->pict_type);
  583. if(s->pict_type==B_TYPE || s->low_delay){
  584. *pict= *(AVFrame*)&s->current_picture;
  585. ff_print_debug_info(s, s->current_picture_ptr);
  586. } else {
  587. *pict= *(AVFrame*)&s->last_picture;
  588. ff_print_debug_info(s, s->last_picture_ptr);
  589. }
  590. /* Return the Picture timestamp as the frame number */
  591. /* we substract 1 because it is added on utils.c */
  592. avctx->frame_number = s->picture_number - 1;
  593. /* dont output the last pic after seeking */
  594. if(s->last_picture_ptr || s->low_delay)
  595. *data_size = sizeof(AVFrame);
  596. #ifdef PRINT_FRAME_TIME
  597. printf("%Ld\n", rdtsc()-time);
  598. #endif
  599. return get_consumed_bytes(s, buf_size);
  600. }
  601. static const AVOption mpeg4_decoptions[] =
  602. {
  603. AVOPTION_SUB(avoptions_workaround_bug),
  604. AVOPTION_END()
  605. };
  606. AVCodec mpeg4_decoder = {
  607. "mpeg4",
  608. CODEC_TYPE_VIDEO,
  609. CODEC_ID_MPEG4,
  610. sizeof(MpegEncContext),
  611. ff_h263_decode_init,
  612. NULL,
  613. ff_h263_decode_end,
  614. ff_h263_decode_frame,
  615. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
  616. .options = mpeg4_decoptions,
  617. .flush= ff_mpeg_flush,
  618. };
  619. AVCodec h263_decoder = {
  620. "h263",
  621. CODEC_TYPE_VIDEO,
  622. CODEC_ID_H263,
  623. sizeof(MpegEncContext),
  624. ff_h263_decode_init,
  625. NULL,
  626. ff_h263_decode_end,
  627. ff_h263_decode_frame,
  628. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
  629. .flush= ff_mpeg_flush,
  630. };
  631. AVCodec msmpeg4v1_decoder = {
  632. "msmpeg4v1",
  633. CODEC_TYPE_VIDEO,
  634. CODEC_ID_MSMPEG4V1,
  635. sizeof(MpegEncContext),
  636. ff_h263_decode_init,
  637. NULL,
  638. ff_h263_decode_end,
  639. ff_h263_decode_frame,
  640. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  641. mpeg4_decoptions,
  642. };
  643. AVCodec msmpeg4v2_decoder = {
  644. "msmpeg4v2",
  645. CODEC_TYPE_VIDEO,
  646. CODEC_ID_MSMPEG4V2,
  647. sizeof(MpegEncContext),
  648. ff_h263_decode_init,
  649. NULL,
  650. ff_h263_decode_end,
  651. ff_h263_decode_frame,
  652. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  653. mpeg4_decoptions,
  654. };
  655. AVCodec msmpeg4v3_decoder = {
  656. "msmpeg4",
  657. CODEC_TYPE_VIDEO,
  658. CODEC_ID_MSMPEG4V3,
  659. sizeof(MpegEncContext),
  660. ff_h263_decode_init,
  661. NULL,
  662. ff_h263_decode_end,
  663. ff_h263_decode_frame,
  664. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  665. .options = mpeg4_decoptions,
  666. };
  667. AVCodec wmv1_decoder = {
  668. "wmv1",
  669. CODEC_TYPE_VIDEO,
  670. CODEC_ID_WMV1,
  671. sizeof(MpegEncContext),
  672. ff_h263_decode_init,
  673. NULL,
  674. ff_h263_decode_end,
  675. ff_h263_decode_frame,
  676. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  677. mpeg4_decoptions,
  678. };
  679. AVCodec h263i_decoder = {
  680. "h263i",
  681. CODEC_TYPE_VIDEO,
  682. CODEC_ID_H263I,
  683. sizeof(MpegEncContext),
  684. ff_h263_decode_init,
  685. NULL,
  686. ff_h263_decode_end,
  687. ff_h263_decode_frame,
  688. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  689. mpeg4_decoptions,
  690. };
  691. AVCodec flv_decoder = {
  692. "flv",
  693. CODEC_TYPE_VIDEO,
  694. CODEC_ID_FLV1,
  695. sizeof(MpegEncContext),
  696. ff_h263_decode_init,
  697. NULL,
  698. ff_h263_decode_end,
  699. ff_h263_decode_frame,
  700. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1
  701. };