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.

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