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.

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