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.

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