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.

884 lines
27KB

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