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