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.

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