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.

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