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.

897 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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_VC1:
  81. case CODEC_ID_WMV3:
  82. s->h263_msmpeg4 = 1;
  83. s->h263_pred = 1;
  84. s->msmpeg4_version=6;
  85. break;
  86. case CODEC_ID_H263I:
  87. break;
  88. case CODEC_ID_FLV1:
  89. s->h263_flv = 1;
  90. break;
  91. default:
  92. return -1;
  93. }
  94. s->codec_id= avctx->codec->id;
  95. /* for h263, we allocate the images after having read the header */
  96. if (avctx->codec->id != CODEC_ID_H263 && avctx->codec->id != CODEC_ID_MPEG4)
  97. if (MPV_common_init(s) < 0)
  98. return -1;
  99. if (s->h263_msmpeg4)
  100. ff_msmpeg4_decode_init(s);
  101. else
  102. h263_decode_init_vlc(s);
  103. return 0;
  104. }
  105. int ff_h263_decode_end(AVCodecContext *avctx)
  106. {
  107. MpegEncContext *s = avctx->priv_data;
  108. MPV_common_end(s);
  109. return 0;
  110. }
  111. /**
  112. * returns the number of bytes consumed for building the current frame
  113. */
  114. static int get_consumed_bytes(MpegEncContext *s, int buf_size){
  115. int pos= (get_bits_count(&s->gb)+7)>>3;
  116. if(s->divx_packed){
  117. //we would have to scan through the whole buf to handle the weird reordering ...
  118. return buf_size;
  119. }else if(s->flags&CODEC_FLAG_TRUNCATED){
  120. pos -= s->parse_context.last_index;
  121. if(pos<0) pos=0; // padding is not really read so this might be -1
  122. return pos;
  123. }else{
  124. if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
  125. if(pos+10>buf_size) pos=buf_size; // oops ;)
  126. return pos;
  127. }
  128. }
  129. static int decode_slice(MpegEncContext *s){
  130. const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
  131. const int mb_size= 16>>s->avctx->lowres;
  132. s->last_resync_gb= s->gb;
  133. s->first_slice_line= 1;
  134. s->resync_mb_x= s->mb_x;
  135. s->resync_mb_y= s->mb_y;
  136. ff_set_qscale(s, s->qscale);
  137. if(s->partitioned_frame){
  138. const int qscale= s->qscale;
  139. if(s->codec_id==CODEC_ID_MPEG4){
  140. if(ff_mpeg4_decode_partitions(s) < 0)
  141. return -1;
  142. }
  143. /* restore variables which were modified */
  144. s->first_slice_line=1;
  145. s->mb_x= s->resync_mb_x;
  146. s->mb_y= s->resync_mb_y;
  147. ff_set_qscale(s, qscale);
  148. }
  149. for(; s->mb_y < s->mb_height; s->mb_y++) {
  150. /* per-row end of slice checks */
  151. if(s->msmpeg4_version){
  152. if(s->resync_mb_y + s->slice_height == s->mb_y){
  153. 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);
  154. return 0;
  155. }
  156. }
  157. if(s->msmpeg4_version==1){
  158. s->last_dc[0]=
  159. s->last_dc[1]=
  160. s->last_dc[2]= 128;
  161. }
  162. ff_init_block_index(s);
  163. for(; s->mb_x < s->mb_width; s->mb_x++) {
  164. int ret;
  165. ff_update_block_index(s);
  166. if(s->resync_mb_x == s->mb_x && s->resync_mb_y+1 == s->mb_y){
  167. s->first_slice_line=0;
  168. }
  169. /* DCT & quantize */
  170. s->mv_dir = MV_DIR_FORWARD;
  171. s->mv_type = MV_TYPE_16X16;
  172. // s->mb_skipped = 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 don't 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 does not 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_SKIPPED) 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->avctx->codec_tag == ff_get_fourcc("RMP4"))
  434. s->xvid_build= -1;
  435. #if 0
  436. if(s->avctx->codec_tag == ff_get_fourcc("DIVX") && s->vo_type==0 && s->vol_control_parameters==1
  437. && s->padding_bug_score > 0 && s->low_delay) // XVID with modified fourcc
  438. s->xvid_build= -1;
  439. #endif
  440. }
  441. if(s->xvid_build==0 && s->divx_version==0 && s->lavc_build==0){
  442. if(s->avctx->codec_tag == ff_get_fourcc("DIVX") && s->vo_type==0 && s->vol_control_parameters==0)
  443. s->divx_version= 400; //divx 4
  444. }
  445. if(s->xvid_build && s->divx_version){
  446. s->divx_version=
  447. s->divx_build= 0;
  448. }
  449. if(s->workaround_bugs&FF_BUG_AUTODETECT){
  450. if(s->avctx->codec_tag == ff_get_fourcc("XVIX"))
  451. s->workaround_bugs|= FF_BUG_XVID_ILACE;
  452. if(s->avctx->codec_tag == ff_get_fourcc("UMP4")){
  453. s->workaround_bugs|= FF_BUG_UMP4;
  454. }
  455. if(s->divx_version>=500){
  456. s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
  457. }
  458. if(s->divx_version>502){
  459. s->workaround_bugs|= FF_BUG_QPEL_CHROMA2;
  460. }
  461. if(s->xvid_build && s->xvid_build<=3)
  462. s->padding_bug_score= 256*256*256*64;
  463. if(s->xvid_build && s->xvid_build<=1)
  464. s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
  465. if(s->xvid_build && s->xvid_build<=12)
  466. s->workaround_bugs|= FF_BUG_EDGE;
  467. if(s->xvid_build && s->xvid_build<=32)
  468. s->workaround_bugs|= FF_BUG_DC_CLIP;
  469. #define SET_QPEL_FUNC(postfix1, postfix2) \
  470. s->dsp.put_ ## postfix1 = ff_put_ ## postfix2;\
  471. s->dsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2;\
  472. s->dsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
  473. if(s->lavc_build && s->lavc_build<4653)
  474. s->workaround_bugs|= FF_BUG_STD_QPEL;
  475. if(s->lavc_build && s->lavc_build<4655)
  476. s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
  477. if(s->lavc_build && s->lavc_build<4670){
  478. s->workaround_bugs|= FF_BUG_EDGE;
  479. }
  480. if(s->lavc_build && s->lavc_build<=4712)
  481. s->workaround_bugs|= FF_BUG_DC_CLIP;
  482. if(s->divx_version)
  483. s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
  484. //printf("padding_bug_score: %d\n", s->padding_bug_score);
  485. if(s->divx_version==501 && s->divx_build==20020416)
  486. s->padding_bug_score= 256*256*256*64;
  487. if(s->divx_version && s->divx_version<500){
  488. s->workaround_bugs|= FF_BUG_EDGE;
  489. }
  490. if(s->divx_version)
  491. s->workaround_bugs|= FF_BUG_HPEL_CHROMA;
  492. #if 0
  493. if(s->divx_version==500)
  494. s->padding_bug_score= 256*256*256*64;
  495. /* very ugly XVID padding bug detection FIXME/XXX solve this differently
  496. * lets hope this at least works
  497. */
  498. if( s->resync_marker==0 && s->data_partitioning==0 && s->divx_version==0
  499. && s->codec_id==CODEC_ID_MPEG4 && s->vo_type==0)
  500. s->workaround_bugs|= FF_BUG_NO_PADDING;
  501. if(s->lavc_build && s->lavc_build<4609) //FIXME not sure about the version num but a 4609 file seems ok
  502. s->workaround_bugs|= FF_BUG_NO_PADDING;
  503. #endif
  504. }
  505. if(s->workaround_bugs& FF_BUG_STD_QPEL){
  506. SET_QPEL_FUNC(qpel_pixels_tab[0][ 5], qpel16_mc11_old_c)
  507. SET_QPEL_FUNC(qpel_pixels_tab[0][ 7], qpel16_mc31_old_c)
  508. SET_QPEL_FUNC(qpel_pixels_tab[0][ 9], qpel16_mc12_old_c)
  509. SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
  510. SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
  511. SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
  512. SET_QPEL_FUNC(qpel_pixels_tab[1][ 5], qpel8_mc11_old_c)
  513. SET_QPEL_FUNC(qpel_pixels_tab[1][ 7], qpel8_mc31_old_c)
  514. SET_QPEL_FUNC(qpel_pixels_tab[1][ 9], qpel8_mc12_old_c)
  515. SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
  516. SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
  517. SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
  518. }
  519. if(avctx->debug & FF_DEBUG_BUGS)
  520. av_log(s->avctx, AV_LOG_DEBUG, "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
  521. s->workaround_bugs, s->lavc_build, s->xvid_build, s->divx_version, s->divx_build,
  522. s->divx_packed ? "p" : "");
  523. #if 0 // dump bits per frame / qp / complexity
  524. {
  525. static FILE *f=NULL;
  526. if(!f) f=fopen("rate_qp_cplx.txt", "w");
  527. fprintf(f, "%d %d %f\n", buf_size, s->qscale, buf_size*(double)s->qscale);
  528. }
  529. #endif
  530. #if defined(HAVE_MMX) && defined(CONFIG_GPL)
  531. if(s->codec_id == CODEC_ID_MPEG4 && s->xvid_build && avctx->idct_algo == FF_IDCT_AUTO && (mm_flags & MM_MMX)){
  532. avctx->idct_algo= FF_IDCT_XVIDMMX;
  533. avctx->coded_width= 0; // force reinit
  534. // dsputil_init(&s->dsp, avctx);
  535. s->picture_number=0;
  536. }
  537. #endif
  538. /* After H263 & mpeg4 header decode we have the height, width,*/
  539. /* and other parameters. So then we could init the picture */
  540. /* FIXME: By the way H263 decoder is evolving it should have */
  541. /* an H263EncContext */
  542. if ( s->width != avctx->coded_width
  543. || s->height != avctx->coded_height) {
  544. /* H.263 could change picture size any time */
  545. ParseContext pc= s->parse_context; //FIXME move these demuxng hack to avformat
  546. s->parse_context.buffer=0;
  547. MPV_common_end(s);
  548. s->parse_context= pc;
  549. }
  550. if (!s->context_initialized) {
  551. avcodec_set_dimensions(avctx, s->width, s->height);
  552. goto retry;
  553. }
  554. if((s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P))
  555. s->gob_index = ff_h263_get_gob_height(s);
  556. // for hurry_up==5
  557. s->current_picture.pict_type= s->pict_type;
  558. s->current_picture.key_frame= s->pict_type == I_TYPE;
  559. /* skip B-frames if we don't have reference frames */
  560. if(s->last_picture_ptr==NULL && (s->pict_type==B_TYPE || s->dropable)) return get_consumed_bytes(s, buf_size);
  561. /* skip b frames if we are in a hurry */
  562. if(avctx->hurry_up && s->pict_type==B_TYPE) return get_consumed_bytes(s, buf_size);
  563. if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==B_TYPE)
  564. || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=I_TYPE)
  565. || avctx->skip_frame >= AVDISCARD_ALL)
  566. return get_consumed_bytes(s, buf_size);
  567. /* skip everything if we are in a hurry>=5 */
  568. if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size);
  569. if(s->next_p_frame_damaged){
  570. if(s->pict_type==B_TYPE)
  571. return get_consumed_bytes(s, buf_size);
  572. else
  573. s->next_p_frame_damaged=0;
  574. }
  575. if(MPV_frame_start(s, avctx) < 0)
  576. return -1;
  577. #ifdef DEBUG
  578. av_log(avctx, AV_LOG_DEBUG, "qscale=%d\n", s->qscale);
  579. #endif
  580. ff_er_frame_start(s);
  581. //the second part of the wmv2 header contains the MB skip bits which are stored in current_picture->mb_type
  582. //which isnt available before MPV_frame_start()
  583. if (s->msmpeg4_version==5){
  584. if(ff_wmv2_decode_secondary_picture_header(s) < 0)
  585. return -1;
  586. }
  587. /* decode each macroblock */
  588. s->mb_x=0;
  589. s->mb_y=0;
  590. decode_slice(s);
  591. while(s->mb_y<s->mb_height){
  592. if(s->msmpeg4_version){
  593. if(s->mb_x!=0 || (s->mb_y%s->slice_height)!=0 || get_bits_count(&s->gb) > s->gb.size_in_bits)
  594. break;
  595. }else{
  596. if(ff_h263_resync(s)<0)
  597. break;
  598. }
  599. if(s->msmpeg4_version<4 && s->h263_pred)
  600. ff_mpeg4_clean_buffers(s);
  601. decode_slice(s);
  602. }
  603. if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==I_TYPE)
  604. if(msmpeg4_decode_ext_header(s, buf_size) < 0){
  605. s->error_status_table[s->mb_num-1]= AC_ERROR|DC_ERROR|MV_ERROR;
  606. }
  607. /* divx 5.01+ bistream reorder stuff */
  608. if(s->codec_id==CODEC_ID_MPEG4 && s->bitstream_buffer_size==0 && s->divx_packed){
  609. int current_pos= get_bits_count(&s->gb)>>3;
  610. int startcode_found=0;
  611. if(buf_size - current_pos > 5){
  612. int i;
  613. for(i=current_pos; i<buf_size-3; i++){
  614. if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==1 && buf[i+3]==0xB6){
  615. startcode_found=1;
  616. break;
  617. }
  618. }
  619. }
  620. if(s->gb.buffer == s->bitstream_buffer && buf_size>20){ //xvid style
  621. startcode_found=1;
  622. current_pos=0;
  623. }
  624. if(startcode_found){
  625. s->bitstream_buffer= av_fast_realloc(
  626. s->bitstream_buffer,
  627. &s->allocated_bitstream_buffer_size,
  628. buf_size - current_pos + FF_INPUT_BUFFER_PADDING_SIZE);
  629. memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
  630. s->bitstream_buffer_size= buf_size - current_pos;
  631. }
  632. }
  633. ff_er_frame_end(s);
  634. MPV_frame_end(s);
  635. assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
  636. assert(s->current_picture.pict_type == s->pict_type);
  637. if (s->pict_type == B_TYPE || s->low_delay) {
  638. *pict= *(AVFrame*)s->current_picture_ptr;
  639. } else if (s->last_picture_ptr != NULL) {
  640. *pict= *(AVFrame*)s->last_picture_ptr;
  641. }
  642. if(s->last_picture_ptr || s->low_delay){
  643. *data_size = sizeof(AVFrame);
  644. ff_print_debug_info(s, pict);
  645. }
  646. /* Return the Picture timestamp as the frame number */
  647. /* we substract 1 because it is added on utils.c */
  648. avctx->frame_number = s->picture_number - 1;
  649. #ifdef PRINT_FRAME_TIME
  650. av_log(avctx, AV_LOG_DEBUG, "%Ld\n", rdtsc()-time);
  651. #endif
  652. return get_consumed_bytes(s, buf_size);
  653. }
  654. AVCodec mpeg4_decoder = {
  655. "mpeg4",
  656. CODEC_TYPE_VIDEO,
  657. CODEC_ID_MPEG4,
  658. sizeof(MpegEncContext),
  659. ff_h263_decode_init,
  660. NULL,
  661. ff_h263_decode_end,
  662. ff_h263_decode_frame,
  663. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  664. .flush= ff_mpeg_flush,
  665. };
  666. AVCodec h263_decoder = {
  667. "h263",
  668. CODEC_TYPE_VIDEO,
  669. CODEC_ID_H263,
  670. sizeof(MpegEncContext),
  671. ff_h263_decode_init,
  672. NULL,
  673. ff_h263_decode_end,
  674. ff_h263_decode_frame,
  675. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  676. .flush= ff_mpeg_flush,
  677. };
  678. AVCodec msmpeg4v1_decoder = {
  679. "msmpeg4v1",
  680. CODEC_TYPE_VIDEO,
  681. CODEC_ID_MSMPEG4V1,
  682. sizeof(MpegEncContext),
  683. ff_h263_decode_init,
  684. NULL,
  685. ff_h263_decode_end,
  686. ff_h263_decode_frame,
  687. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  688. };
  689. AVCodec msmpeg4v2_decoder = {
  690. "msmpeg4v2",
  691. CODEC_TYPE_VIDEO,
  692. CODEC_ID_MSMPEG4V2,
  693. sizeof(MpegEncContext),
  694. ff_h263_decode_init,
  695. NULL,
  696. ff_h263_decode_end,
  697. ff_h263_decode_frame,
  698. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  699. };
  700. AVCodec msmpeg4v3_decoder = {
  701. "msmpeg4",
  702. CODEC_TYPE_VIDEO,
  703. CODEC_ID_MSMPEG4V3,
  704. sizeof(MpegEncContext),
  705. ff_h263_decode_init,
  706. NULL,
  707. ff_h263_decode_end,
  708. ff_h263_decode_frame,
  709. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  710. };
  711. AVCodec wmv1_decoder = {
  712. "wmv1",
  713. CODEC_TYPE_VIDEO,
  714. CODEC_ID_WMV1,
  715. sizeof(MpegEncContext),
  716. ff_h263_decode_init,
  717. NULL,
  718. ff_h263_decode_end,
  719. ff_h263_decode_frame,
  720. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  721. };
  722. AVCodec h263i_decoder = {
  723. "h263i",
  724. CODEC_TYPE_VIDEO,
  725. CODEC_ID_H263I,
  726. sizeof(MpegEncContext),
  727. ff_h263_decode_init,
  728. NULL,
  729. ff_h263_decode_end,
  730. ff_h263_decode_frame,
  731. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  732. };
  733. AVCodec flv_decoder = {
  734. "flv",
  735. CODEC_TYPE_VIDEO,
  736. CODEC_ID_FLV1,
  737. sizeof(MpegEncContext),
  738. ff_h263_decode_init,
  739. NULL,
  740. ff_h263_decode_end,
  741. ff_h263_decode_frame,
  742. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1
  743. };
  744. AVCodecParser h263_parser = {
  745. { CODEC_ID_H263 },
  746. sizeof(ParseContext),
  747. NULL,
  748. h263_parse,
  749. ff_parse_close,
  750. };