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.

893 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->coded_width;
  35. s->height = avctx->coded_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. const int mb_size= 16>>s->avctx->lowres;
  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*mb_size, mb_size);
  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*mb_size, mb_size);
  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. int ff_mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size){
  258. int vop_found, i;
  259. uint32_t state;
  260. vop_found= pc->frame_start_found;
  261. state= pc->state;
  262. i=0;
  263. if(!vop_found){
  264. for(i=0; i<buf_size; i++){
  265. state= (state<<8) | buf[i];
  266. if(state == 0x1B6){
  267. i++;
  268. vop_found=1;
  269. break;
  270. }
  271. }
  272. }
  273. if(vop_found){
  274. /* EOF considered as end of frame */
  275. if (buf_size == 0)
  276. return 0;
  277. for(; i<buf_size; i++){
  278. state= (state<<8) | buf[i];
  279. if((state&0xFFFFFF00) == 0x100){
  280. pc->frame_start_found=0;
  281. pc->state=-1;
  282. return i-3;
  283. }
  284. }
  285. }
  286. pc->frame_start_found= vop_found;
  287. pc->state= state;
  288. return END_NOT_FOUND;
  289. }
  290. static int h263_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size){
  291. int vop_found, i;
  292. uint32_t state;
  293. vop_found= pc->frame_start_found;
  294. state= pc->state;
  295. i=0;
  296. if(!vop_found){
  297. for(i=0; i<buf_size; i++){
  298. state= (state<<8) | buf[i];
  299. if(state>>(32-22) == 0x20){
  300. i++;
  301. vop_found=1;
  302. break;
  303. }
  304. }
  305. }
  306. if(vop_found){
  307. for(; i<buf_size; i++){
  308. state= (state<<8) | buf[i];
  309. if(state>>(32-22) == 0x20){
  310. pc->frame_start_found=0;
  311. pc->state=-1;
  312. return i-3;
  313. }
  314. }
  315. }
  316. pc->frame_start_found= vop_found;
  317. pc->state= state;
  318. return END_NOT_FOUND;
  319. }
  320. static int h263_parse(AVCodecParserContext *s,
  321. AVCodecContext *avctx,
  322. uint8_t **poutbuf, int *poutbuf_size,
  323. const uint8_t *buf, int buf_size)
  324. {
  325. ParseContext *pc = s->priv_data;
  326. int next;
  327. next= h263_find_frame_end(pc, buf, buf_size);
  328. if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  329. *poutbuf = NULL;
  330. *poutbuf_size = 0;
  331. return buf_size;
  332. }
  333. *poutbuf = (uint8_t *)buf;
  334. *poutbuf_size = buf_size;
  335. return next;
  336. }
  337. int ff_h263_decode_frame(AVCodecContext *avctx,
  338. void *data, int *data_size,
  339. uint8_t *buf, int buf_size)
  340. {
  341. MpegEncContext *s = avctx->priv_data;
  342. int ret;
  343. AVFrame *pict = data;
  344. #ifdef PRINT_FRAME_TIME
  345. uint64_t time= rdtsc();
  346. #endif
  347. #ifdef DEBUG
  348. printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
  349. printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
  350. #endif
  351. s->flags= avctx->flags;
  352. s->flags2= avctx->flags2;
  353. /* no supplementary picture */
  354. if (buf_size == 0) {
  355. /* special case for last picture */
  356. if (s->low_delay==0 && s->next_picture_ptr) {
  357. *pict= *(AVFrame*)s->next_picture_ptr;
  358. s->next_picture_ptr= NULL;
  359. *data_size = sizeof(AVFrame);
  360. }
  361. return 0;
  362. }
  363. if(s->flags&CODEC_FLAG_TRUNCATED){
  364. int next;
  365. if(s->codec_id==CODEC_ID_MPEG4){
  366. next= ff_mpeg4_find_frame_end(&s->parse_context, buf, buf_size);
  367. }else if(s->codec_id==CODEC_ID_H263){
  368. next= h263_find_frame_end(&s->parse_context, buf, buf_size);
  369. }else{
  370. av_log(s->avctx, AV_LOG_ERROR, "this codec doesnt support truncated bitstreams\n");
  371. return -1;
  372. }
  373. if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 )
  374. return buf_size;
  375. }
  376. retry:
  377. if(s->bitstream_buffer_size && (s->divx_packed || buf_size<20)){ //divx 5.01+/xvid frame reorder
  378. init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size*8);
  379. }else
  380. init_get_bits(&s->gb, buf, buf_size*8);
  381. s->bitstream_buffer_size=0;
  382. if (!s->context_initialized) {
  383. if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
  384. return -1;
  385. }
  386. //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
  387. if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
  388. int i= ff_find_unused_picture(s, 0);
  389. s->current_picture_ptr= &s->picture[i];
  390. }
  391. /* let's go :-) */
  392. if (s->msmpeg4_version==5) {
  393. ret= ff_wmv2_decode_picture_header(s);
  394. } else if (s->msmpeg4_version) {
  395. ret = msmpeg4_decode_picture_header(s);
  396. } else if (s->h263_pred) {
  397. if(s->avctx->extradata_size && s->picture_number==0){
  398. GetBitContext gb;
  399. init_get_bits(&gb, s->avctx->extradata, s->avctx->extradata_size*8);
  400. ret = ff_mpeg4_decode_picture_header(s, &gb);
  401. }
  402. ret = ff_mpeg4_decode_picture_header(s, &s->gb);
  403. if(s->flags& CODEC_FLAG_LOW_DELAY)
  404. s->low_delay=1;
  405. } else if (s->codec_id == CODEC_ID_H263I) {
  406. ret = intel_h263_decode_picture_header(s);
  407. } else if (s->h263_flv) {
  408. ret = flv_h263_decode_picture_header(s);
  409. } else {
  410. ret = h263_decode_picture_header(s);
  411. }
  412. if(ret==FRAME_SKIPED) return get_consumed_bytes(s, buf_size);
  413. /* skip if the header was thrashed */
  414. if (ret < 0){
  415. av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
  416. return -1;
  417. }
  418. avctx->has_b_frames= !s->low_delay;
  419. if(s->xvid_build==0 && s->divx_version==0 && s->lavc_build==0){
  420. if(s->avctx->stream_codec_tag == ff_get_fourcc("XVID") ||
  421. s->avctx->codec_tag == ff_get_fourcc("XVID") || s->avctx->codec_tag == ff_get_fourcc("XVIX"))
  422. s->xvid_build= -1;
  423. #if 0
  424. if(s->avctx->codec_tag == ff_get_fourcc("DIVX") && s->vo_type==0 && s->vol_control_parameters==1
  425. && s->padding_bug_score > 0 && s->low_delay) // XVID with modified fourcc
  426. s->xvid_build= -1;
  427. #endif
  428. }
  429. if(s->xvid_build==0 && s->divx_version==0 && s->lavc_build==0){
  430. if(s->avctx->codec_tag == ff_get_fourcc("DIVX") && s->vo_type==0 && s->vol_control_parameters==0)
  431. s->divx_version= 400; //divx 4
  432. }
  433. if(s->xvid_build && s->divx_version){
  434. s->divx_version=
  435. s->divx_build= 0;
  436. }
  437. if(s->workaround_bugs&FF_BUG_AUTODETECT){
  438. s->workaround_bugs &= ~FF_BUG_NO_PADDING;
  439. if(s->padding_bug_score > -2 && !s->data_partitioning && (s->divx_version || !s->resync_marker))
  440. s->workaround_bugs |= FF_BUG_NO_PADDING;
  441. if(s->avctx->codec_tag == ff_get_fourcc("XVIX"))
  442. s->workaround_bugs|= FF_BUG_XVID_ILACE;
  443. if(s->avctx->codec_tag == ff_get_fourcc("UMP4")){
  444. s->workaround_bugs|= FF_BUG_UMP4;
  445. }
  446. if(s->divx_version>=500){
  447. s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
  448. }
  449. if(s->divx_version>502){
  450. s->workaround_bugs|= FF_BUG_QPEL_CHROMA2;
  451. }
  452. if(s->xvid_build && s->xvid_build<=3)
  453. s->padding_bug_score= 256*256*256*64;
  454. if(s->xvid_build && s->xvid_build<=1)
  455. s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
  456. if(s->xvid_build && s->xvid_build<=12)
  457. s->workaround_bugs|= FF_BUG_EDGE;
  458. if(s->xvid_build && s->xvid_build<=32)
  459. s->workaround_bugs|= FF_BUG_DC_CLIP;
  460. #define SET_QPEL_FUNC(postfix1, postfix2) \
  461. s->dsp.put_ ## postfix1 = ff_put_ ## postfix2;\
  462. s->dsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2;\
  463. s->dsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
  464. if(s->lavc_build && s->lavc_build<4653)
  465. s->workaround_bugs|= FF_BUG_STD_QPEL;
  466. if(s->lavc_build && s->lavc_build<4655)
  467. s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
  468. if(s->lavc_build && s->lavc_build<4670){
  469. s->workaround_bugs|= FF_BUG_EDGE;
  470. }
  471. if(s->lavc_build && s->lavc_build<=4712)
  472. s->workaround_bugs|= FF_BUG_DC_CLIP;
  473. if(s->divx_version)
  474. s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
  475. //printf("padding_bug_score: %d\n", s->padding_bug_score);
  476. if(s->divx_version==501 && s->divx_build==20020416)
  477. s->padding_bug_score= 256*256*256*64;
  478. if(s->divx_version && s->divx_version<500){
  479. s->workaround_bugs|= FF_BUG_EDGE;
  480. }
  481. if(s->divx_version)
  482. s->workaround_bugs|= FF_BUG_HPEL_CHROMA;
  483. #if 0
  484. if(s->divx_version==500)
  485. s->padding_bug_score= 256*256*256*64;
  486. /* very ugly XVID padding bug detection FIXME/XXX solve this differently
  487. * lets hope this at least works
  488. */
  489. if( s->resync_marker==0 && s->data_partitioning==0 && s->divx_version==0
  490. && s->codec_id==CODEC_ID_MPEG4 && s->vo_type==0)
  491. s->workaround_bugs|= FF_BUG_NO_PADDING;
  492. if(s->lavc_build && s->lavc_build<4609) //FIXME not sure about the version num but a 4609 file seems ok
  493. s->workaround_bugs|= FF_BUG_NO_PADDING;
  494. #endif
  495. }
  496. if(s->workaround_bugs& FF_BUG_STD_QPEL){
  497. SET_QPEL_FUNC(qpel_pixels_tab[0][ 5], qpel16_mc11_old_c)
  498. SET_QPEL_FUNC(qpel_pixels_tab[0][ 7], qpel16_mc31_old_c)
  499. SET_QPEL_FUNC(qpel_pixels_tab[0][ 9], qpel16_mc12_old_c)
  500. SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
  501. SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
  502. SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
  503. SET_QPEL_FUNC(qpel_pixels_tab[1][ 5], qpel8_mc11_old_c)
  504. SET_QPEL_FUNC(qpel_pixels_tab[1][ 7], qpel8_mc31_old_c)
  505. SET_QPEL_FUNC(qpel_pixels_tab[1][ 9], qpel8_mc12_old_c)
  506. SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
  507. SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
  508. SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
  509. }
  510. if(avctx->debug & FF_DEBUG_BUGS)
  511. av_log(s->avctx, AV_LOG_DEBUG, "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
  512. s->workaround_bugs, s->lavc_build, s->xvid_build, s->divx_version, s->divx_build,
  513. s->divx_packed ? "p" : "");
  514. #if 0 // dump bits per frame / qp / complexity
  515. {
  516. static FILE *f=NULL;
  517. if(!f) f=fopen("rate_qp_cplx.txt", "w");
  518. fprintf(f, "%d %d %f\n", buf_size, s->qscale, buf_size*(double)s->qscale);
  519. }
  520. #endif
  521. #ifdef HAVE_MMX
  522. 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)){
  523. avctx->idct_algo= FF_IDCT_LIBMPEG2MMX;
  524. avctx->coded_width= 0; // force reinit
  525. }
  526. #endif
  527. /* After H263 & mpeg4 header decode we have the height, width,*/
  528. /* and other parameters. So then we could init the picture */
  529. /* FIXME: By the way H263 decoder is evolving it should have */
  530. /* an H263EncContext */
  531. if ( s->width != avctx->coded_width
  532. || s->height != avctx->coded_height) {
  533. /* H.263 could change picture size any time */
  534. ParseContext pc= s->parse_context; //FIXME move these demuxng hack to avformat
  535. s->parse_context.buffer=0;
  536. MPV_common_end(s);
  537. s->parse_context= pc;
  538. }
  539. if (!s->context_initialized) {
  540. avcodec_set_dimensions(avctx, s->width, s->height);
  541. goto retry;
  542. }
  543. if((s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P))
  544. s->gob_index = ff_h263_get_gob_height(s);
  545. // for hurry_up==5
  546. s->current_picture.pict_type= s->pict_type;
  547. s->current_picture.key_frame= s->pict_type == I_TYPE;
  548. /* skip b frames if we dont have reference frames */
  549. if(s->last_picture_ptr==NULL && (s->pict_type==B_TYPE || s->dropable)) return get_consumed_bytes(s, buf_size);
  550. /* skip b frames if we are in a hurry */
  551. if(avctx->hurry_up && s->pict_type==B_TYPE) return get_consumed_bytes(s, buf_size);
  552. /* skip everything if we are in a hurry>=5 */
  553. if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size);
  554. if(s->next_p_frame_damaged){
  555. if(s->pict_type==B_TYPE)
  556. return get_consumed_bytes(s, buf_size);
  557. else
  558. s->next_p_frame_damaged=0;
  559. }
  560. if(MPV_frame_start(s, avctx) < 0)
  561. return -1;
  562. #ifdef DEBUG
  563. printf("qscale=%d\n", s->qscale);
  564. #endif
  565. ff_er_frame_start(s);
  566. //the second part of the wmv2 header contains the MB skip bits which are stored in current_picture->mb_type
  567. //which isnt available before MPV_frame_start()
  568. if (s->msmpeg4_version==5){
  569. if(ff_wmv2_decode_secondary_picture_header(s) < 0)
  570. return -1;
  571. }
  572. /* decode each macroblock */
  573. s->mb_x=0;
  574. s->mb_y=0;
  575. decode_slice(s);
  576. while(s->mb_y<s->mb_height){
  577. if(s->msmpeg4_version){
  578. if(s->mb_x!=0 || (s->mb_y%s->slice_height)!=0 || get_bits_count(&s->gb) > s->gb.size_in_bits)
  579. break;
  580. }else{
  581. if(ff_h263_resync(s)<0)
  582. break;
  583. }
  584. if(s->msmpeg4_version<4 && s->h263_pred)
  585. ff_mpeg4_clean_buffers(s);
  586. decode_slice(s);
  587. }
  588. if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==I_TYPE)
  589. if(msmpeg4_decode_ext_header(s, buf_size) < 0){
  590. s->error_status_table[s->mb_num-1]= AC_ERROR|DC_ERROR|MV_ERROR;
  591. }
  592. /* divx 5.01+ bistream reorder stuff */
  593. if(s->codec_id==CODEC_ID_MPEG4 && s->bitstream_buffer_size==0 && s->divx_packed){
  594. int current_pos= get_bits_count(&s->gb)>>3;
  595. int startcode_found=0;
  596. if( buf_size - current_pos > 5
  597. && buf_size - current_pos < BITSTREAM_BUFFER_SIZE){
  598. int i;
  599. for(i=current_pos; i<buf_size-3; i++){
  600. if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==1 && buf[i+3]==0xB6){
  601. startcode_found=1;
  602. break;
  603. }
  604. }
  605. }
  606. if(s->gb.buffer == s->bitstream_buffer && buf_size>20){ //xvid style
  607. startcode_found=1;
  608. current_pos=0;
  609. }
  610. if(startcode_found){
  611. memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
  612. s->bitstream_buffer_size= buf_size - current_pos;
  613. }
  614. }
  615. ff_er_frame_end(s);
  616. MPV_frame_end(s);
  617. assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
  618. assert(s->current_picture.pict_type == s->pict_type);
  619. if(s->pict_type==B_TYPE || s->low_delay){
  620. *pict= *(AVFrame*)&s->current_picture;
  621. ff_print_debug_info(s, pict);
  622. } else {
  623. *pict= *(AVFrame*)&s->last_picture;
  624. if(pict)
  625. ff_print_debug_info(s, pict);
  626. }
  627. /* Return the Picture timestamp as the frame number */
  628. /* we substract 1 because it is added on utils.c */
  629. avctx->frame_number = s->picture_number - 1;
  630. /* dont output the last pic after seeking */
  631. if(s->last_picture_ptr || s->low_delay)
  632. *data_size = sizeof(AVFrame);
  633. #ifdef PRINT_FRAME_TIME
  634. printf("%Ld\n", rdtsc()-time);
  635. #endif
  636. return get_consumed_bytes(s, buf_size);
  637. }
  638. static const AVOption mpeg4_decoptions[] =
  639. {
  640. AVOPTION_SUB(avoptions_workaround_bug),
  641. AVOPTION_END()
  642. };
  643. AVCodec mpeg4_decoder = {
  644. "mpeg4",
  645. CODEC_TYPE_VIDEO,
  646. CODEC_ID_MPEG4,
  647. sizeof(MpegEncContext),
  648. ff_h263_decode_init,
  649. NULL,
  650. ff_h263_decode_end,
  651. ff_h263_decode_frame,
  652. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
  653. .options = mpeg4_decoptions,
  654. .flush= ff_mpeg_flush,
  655. };
  656. AVCodec h263_decoder = {
  657. "h263",
  658. CODEC_TYPE_VIDEO,
  659. CODEC_ID_H263,
  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 | CODEC_CAP_TRUNCATED,
  666. .flush= ff_mpeg_flush,
  667. };
  668. AVCodec msmpeg4v1_decoder = {
  669. "msmpeg4v1",
  670. CODEC_TYPE_VIDEO,
  671. CODEC_ID_MSMPEG4V1,
  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 msmpeg4v2_decoder = {
  681. "msmpeg4v2",
  682. CODEC_TYPE_VIDEO,
  683. CODEC_ID_MSMPEG4V2,
  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 msmpeg4v3_decoder = {
  693. "msmpeg4",
  694. CODEC_TYPE_VIDEO,
  695. CODEC_ID_MSMPEG4V3,
  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. .options = mpeg4_decoptions,
  703. };
  704. AVCodec wmv1_decoder = {
  705. "wmv1",
  706. CODEC_TYPE_VIDEO,
  707. CODEC_ID_WMV1,
  708. sizeof(MpegEncContext),
  709. ff_h263_decode_init,
  710. NULL,
  711. ff_h263_decode_end,
  712. ff_h263_decode_frame,
  713. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  714. mpeg4_decoptions,
  715. };
  716. AVCodec h263i_decoder = {
  717. "h263i",
  718. CODEC_TYPE_VIDEO,
  719. CODEC_ID_H263I,
  720. sizeof(MpegEncContext),
  721. ff_h263_decode_init,
  722. NULL,
  723. ff_h263_decode_end,
  724. ff_h263_decode_frame,
  725. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  726. mpeg4_decoptions,
  727. };
  728. AVCodec flv_decoder = {
  729. "flv",
  730. CODEC_TYPE_VIDEO,
  731. CODEC_ID_FLV1,
  732. sizeof(MpegEncContext),
  733. ff_h263_decode_init,
  734. NULL,
  735. ff_h263_decode_end,
  736. ff_h263_decode_frame,
  737. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1
  738. };
  739. AVCodecParser h263_parser = {
  740. { CODEC_ID_H263 },
  741. sizeof(ParseContext),
  742. NULL,
  743. h263_parse,
  744. ff_parse_close,
  745. };