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.

914 lines
28KB

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