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.

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