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.

901 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. #ifdef CONFIG_H263_PARSER
  332. static int h263_parse(AVCodecParserContext *s,
  333. AVCodecContext *avctx,
  334. uint8_t **poutbuf, int *poutbuf_size,
  335. const uint8_t *buf, int buf_size)
  336. {
  337. ParseContext *pc = s->priv_data;
  338. int next;
  339. next= h263_find_frame_end(pc, buf, buf_size);
  340. if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
  341. *poutbuf = NULL;
  342. *poutbuf_size = 0;
  343. return buf_size;
  344. }
  345. *poutbuf = (uint8_t *)buf;
  346. *poutbuf_size = buf_size;
  347. return next;
  348. }
  349. #endif
  350. int ff_h263_decode_frame(AVCodecContext *avctx,
  351. void *data, int *data_size,
  352. uint8_t *buf, int buf_size)
  353. {
  354. MpegEncContext *s = avctx->priv_data;
  355. int ret;
  356. AVFrame *pict = data;
  357. #ifdef PRINT_FRAME_TIME
  358. uint64_t time= rdtsc();
  359. #endif
  360. #ifdef DEBUG
  361. av_log(avctx, AV_LOG_DEBUG, "*****frame %d size=%d\n", avctx->frame_number, buf_size);
  362. av_log(avctx, AV_LOG_DEBUG, "bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
  363. #endif
  364. s->flags= avctx->flags;
  365. s->flags2= avctx->flags2;
  366. /* no supplementary picture */
  367. if (buf_size == 0) {
  368. /* special case for last picture */
  369. if (s->low_delay==0 && s->next_picture_ptr) {
  370. *pict= *(AVFrame*)s->next_picture_ptr;
  371. s->next_picture_ptr= NULL;
  372. *data_size = sizeof(AVFrame);
  373. }
  374. return 0;
  375. }
  376. if(s->flags&CODEC_FLAG_TRUNCATED){
  377. int next;
  378. if(s->codec_id==CODEC_ID_MPEG4){
  379. next= ff_mpeg4_find_frame_end(&s->parse_context, buf, buf_size);
  380. }else if(s->codec_id==CODEC_ID_H263){
  381. next= h263_find_frame_end(&s->parse_context, buf, buf_size);
  382. }else{
  383. av_log(s->avctx, AV_LOG_ERROR, "this codec does not support truncated bitstreams\n");
  384. return -1;
  385. }
  386. if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 )
  387. return buf_size;
  388. }
  389. retry:
  390. if(s->bitstream_buffer_size && (s->divx_packed || buf_size<20)){ //divx 5.01+/xvid frame reorder
  391. init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size*8);
  392. }else
  393. init_get_bits(&s->gb, buf, buf_size*8);
  394. s->bitstream_buffer_size=0;
  395. if (!s->context_initialized) {
  396. if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
  397. return -1;
  398. }
  399. //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
  400. if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
  401. int i= ff_find_unused_picture(s, 0);
  402. s->current_picture_ptr= &s->picture[i];
  403. }
  404. /* let's go :-) */
  405. if (s->msmpeg4_version==5) {
  406. ret= ff_wmv2_decode_picture_header(s);
  407. } else if (s->msmpeg4_version) {
  408. ret = msmpeg4_decode_picture_header(s);
  409. } else if (s->h263_pred) {
  410. if(s->avctx->extradata_size && s->picture_number==0){
  411. GetBitContext gb;
  412. init_get_bits(&gb, s->avctx->extradata, s->avctx->extradata_size*8);
  413. ret = ff_mpeg4_decode_picture_header(s, &gb);
  414. }
  415. ret = ff_mpeg4_decode_picture_header(s, &s->gb);
  416. if(s->flags& CODEC_FLAG_LOW_DELAY)
  417. s->low_delay=1;
  418. } else if (s->codec_id == CODEC_ID_H263I) {
  419. ret = intel_h263_decode_picture_header(s);
  420. } else if (s->h263_flv) {
  421. ret = flv_h263_decode_picture_header(s);
  422. } else {
  423. ret = h263_decode_picture_header(s);
  424. }
  425. if(ret==FRAME_SKIPPED) return get_consumed_bytes(s, buf_size);
  426. /* skip if the header was thrashed */
  427. if (ret < 0){
  428. av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
  429. return -1;
  430. }
  431. avctx->has_b_frames= !s->low_delay;
  432. if(s->xvid_build==0 && s->divx_version==0 && s->lavc_build==0){
  433. if(s->avctx->stream_codec_tag == ff_get_fourcc("XVID") ||
  434. s->avctx->codec_tag == ff_get_fourcc("XVID") || s->avctx->codec_tag == ff_get_fourcc("XVIX") ||
  435. s->avctx->codec_tag == ff_get_fourcc("RMP4"))
  436. s->xvid_build= -1;
  437. #if 0
  438. if(s->avctx->codec_tag == ff_get_fourcc("DIVX") && s->vo_type==0 && s->vol_control_parameters==1
  439. && s->padding_bug_score > 0 && s->low_delay) // XVID with modified fourcc
  440. s->xvid_build= -1;
  441. #endif
  442. }
  443. if(s->xvid_build==0 && s->divx_version==0 && s->lavc_build==0){
  444. if(s->avctx->codec_tag == ff_get_fourcc("DIVX") && s->vo_type==0 && s->vol_control_parameters==0)
  445. s->divx_version= 400; //divx 4
  446. }
  447. if(s->xvid_build && s->divx_version){
  448. s->divx_version=
  449. s->divx_build= 0;
  450. }
  451. if(s->workaround_bugs&FF_BUG_AUTODETECT){
  452. if(s->avctx->codec_tag == ff_get_fourcc("XVIX"))
  453. s->workaround_bugs|= FF_BUG_XVID_ILACE;
  454. if(s->avctx->codec_tag == ff_get_fourcc("UMP4")){
  455. s->workaround_bugs|= FF_BUG_UMP4;
  456. }
  457. if(s->divx_version>=500){
  458. s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
  459. }
  460. if(s->divx_version>502){
  461. s->workaround_bugs|= FF_BUG_QPEL_CHROMA2;
  462. }
  463. if(s->xvid_build && s->xvid_build<=3)
  464. s->padding_bug_score= 256*256*256*64;
  465. if(s->xvid_build && s->xvid_build<=1)
  466. s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
  467. if(s->xvid_build && s->xvid_build<=12)
  468. s->workaround_bugs|= FF_BUG_EDGE;
  469. if(s->xvid_build && s->xvid_build<=32)
  470. s->workaround_bugs|= FF_BUG_DC_CLIP;
  471. #define SET_QPEL_FUNC(postfix1, postfix2) \
  472. s->dsp.put_ ## postfix1 = ff_put_ ## postfix2;\
  473. s->dsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2;\
  474. s->dsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
  475. if(s->lavc_build && s->lavc_build<4653)
  476. s->workaround_bugs|= FF_BUG_STD_QPEL;
  477. if(s->lavc_build && s->lavc_build<4655)
  478. s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
  479. if(s->lavc_build && s->lavc_build<4670){
  480. s->workaround_bugs|= FF_BUG_EDGE;
  481. }
  482. if(s->lavc_build && s->lavc_build<=4712)
  483. s->workaround_bugs|= FF_BUG_DC_CLIP;
  484. if(s->divx_version)
  485. s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
  486. //printf("padding_bug_score: %d\n", s->padding_bug_score);
  487. if(s->divx_version==501 && s->divx_build==20020416)
  488. s->padding_bug_score= 256*256*256*64;
  489. if(s->divx_version && s->divx_version<500){
  490. s->workaround_bugs|= FF_BUG_EDGE;
  491. }
  492. if(s->divx_version)
  493. s->workaround_bugs|= FF_BUG_HPEL_CHROMA;
  494. #if 0
  495. if(s->divx_version==500)
  496. s->padding_bug_score= 256*256*256*64;
  497. /* very ugly XVID padding bug detection FIXME/XXX solve this differently
  498. * lets hope this at least works
  499. */
  500. if( s->resync_marker==0 && s->data_partitioning==0 && s->divx_version==0
  501. && s->codec_id==CODEC_ID_MPEG4 && s->vo_type==0)
  502. s->workaround_bugs|= FF_BUG_NO_PADDING;
  503. if(s->lavc_build && s->lavc_build<4609) //FIXME not sure about the version num but a 4609 file seems ok
  504. s->workaround_bugs|= FF_BUG_NO_PADDING;
  505. #endif
  506. }
  507. if(s->workaround_bugs& FF_BUG_STD_QPEL){
  508. SET_QPEL_FUNC(qpel_pixels_tab[0][ 5], qpel16_mc11_old_c)
  509. SET_QPEL_FUNC(qpel_pixels_tab[0][ 7], qpel16_mc31_old_c)
  510. SET_QPEL_FUNC(qpel_pixels_tab[0][ 9], qpel16_mc12_old_c)
  511. SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
  512. SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
  513. SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
  514. SET_QPEL_FUNC(qpel_pixels_tab[1][ 5], qpel8_mc11_old_c)
  515. SET_QPEL_FUNC(qpel_pixels_tab[1][ 7], qpel8_mc31_old_c)
  516. SET_QPEL_FUNC(qpel_pixels_tab[1][ 9], qpel8_mc12_old_c)
  517. SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
  518. SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
  519. SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
  520. }
  521. if(avctx->debug & FF_DEBUG_BUGS)
  522. av_log(s->avctx, AV_LOG_DEBUG, "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
  523. s->workaround_bugs, s->lavc_build, s->xvid_build, s->divx_version, s->divx_build,
  524. s->divx_packed ? "p" : "");
  525. #if 0 // dump bits per frame / qp / complexity
  526. {
  527. static FILE *f=NULL;
  528. if(!f) f=fopen("rate_qp_cplx.txt", "w");
  529. fprintf(f, "%d %d %f\n", buf_size, s->qscale, buf_size*(double)s->qscale);
  530. }
  531. #endif
  532. #if defined(HAVE_MMX) && defined(CONFIG_GPL)
  533. if(s->codec_id == CODEC_ID_MPEG4 && s->xvid_build && avctx->idct_algo == FF_IDCT_AUTO && (mm_flags & MM_MMX)){
  534. avctx->idct_algo= FF_IDCT_XVIDMMX;
  535. avctx->coded_width= 0; // force reinit
  536. // dsputil_init(&s->dsp, avctx);
  537. s->picture_number=0;
  538. }
  539. #endif
  540. /* After H263 & mpeg4 header decode we have the height, width,*/
  541. /* and other parameters. So then we could init the picture */
  542. /* FIXME: By the way H263 decoder is evolving it should have */
  543. /* an H263EncContext */
  544. if ( s->width != avctx->coded_width
  545. || s->height != avctx->coded_height) {
  546. /* H.263 could change picture size any time */
  547. ParseContext pc= s->parse_context; //FIXME move these demuxng hack to avformat
  548. s->parse_context.buffer=0;
  549. MPV_common_end(s);
  550. s->parse_context= pc;
  551. }
  552. if (!s->context_initialized) {
  553. avcodec_set_dimensions(avctx, s->width, s->height);
  554. goto retry;
  555. }
  556. if((s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P))
  557. s->gob_index = ff_h263_get_gob_height(s);
  558. // for hurry_up==5
  559. s->current_picture.pict_type= s->pict_type;
  560. s->current_picture.key_frame= s->pict_type == I_TYPE;
  561. /* skip B-frames if we don't have reference frames */
  562. if(s->last_picture_ptr==NULL && (s->pict_type==B_TYPE || s->dropable)) return get_consumed_bytes(s, buf_size);
  563. /* skip b frames if we are in a hurry */
  564. if(avctx->hurry_up && s->pict_type==B_TYPE) return get_consumed_bytes(s, buf_size);
  565. if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==B_TYPE)
  566. || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=I_TYPE)
  567. || avctx->skip_frame >= AVDISCARD_ALL)
  568. return get_consumed_bytes(s, buf_size);
  569. /* skip everything if we are in a hurry>=5 */
  570. if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size);
  571. if(s->next_p_frame_damaged){
  572. if(s->pict_type==B_TYPE)
  573. return get_consumed_bytes(s, buf_size);
  574. else
  575. s->next_p_frame_damaged=0;
  576. }
  577. if(MPV_frame_start(s, avctx) < 0)
  578. return -1;
  579. #ifdef DEBUG
  580. av_log(avctx, AV_LOG_DEBUG, "qscale=%d\n", s->qscale);
  581. #endif
  582. ff_er_frame_start(s);
  583. //the second part of the wmv2 header contains the MB skip bits which are stored in current_picture->mb_type
  584. //which isnt available before MPV_frame_start()
  585. if (s->msmpeg4_version==5){
  586. if(ff_wmv2_decode_secondary_picture_header(s) < 0)
  587. return -1;
  588. }
  589. /* decode each macroblock */
  590. s->mb_x=0;
  591. s->mb_y=0;
  592. decode_slice(s);
  593. while(s->mb_y<s->mb_height){
  594. if(s->msmpeg4_version){
  595. if(s->mb_x!=0 || (s->mb_y%s->slice_height)!=0 || get_bits_count(&s->gb) > s->gb.size_in_bits)
  596. break;
  597. }else{
  598. if(ff_h263_resync(s)<0)
  599. break;
  600. }
  601. if(s->msmpeg4_version<4 && s->h263_pred)
  602. ff_mpeg4_clean_buffers(s);
  603. decode_slice(s);
  604. }
  605. if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==I_TYPE)
  606. if(msmpeg4_decode_ext_header(s, buf_size) < 0){
  607. s->error_status_table[s->mb_num-1]= AC_ERROR|DC_ERROR|MV_ERROR;
  608. }
  609. /* divx 5.01+ bistream reorder stuff */
  610. if(s->codec_id==CODEC_ID_MPEG4 && s->bitstream_buffer_size==0 && s->divx_packed){
  611. int current_pos= get_bits_count(&s->gb)>>3;
  612. int startcode_found=0;
  613. if(buf_size - current_pos > 5){
  614. int i;
  615. for(i=current_pos; i<buf_size-3; i++){
  616. if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==1 && buf[i+3]==0xB6){
  617. startcode_found=1;
  618. break;
  619. }
  620. }
  621. }
  622. if(s->gb.buffer == s->bitstream_buffer && buf_size>20){ //xvid style
  623. startcode_found=1;
  624. current_pos=0;
  625. }
  626. if(startcode_found){
  627. s->bitstream_buffer= av_fast_realloc(
  628. s->bitstream_buffer,
  629. &s->allocated_bitstream_buffer_size,
  630. buf_size - current_pos + FF_INPUT_BUFFER_PADDING_SIZE);
  631. memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
  632. s->bitstream_buffer_size= buf_size - current_pos;
  633. }
  634. }
  635. ff_er_frame_end(s);
  636. MPV_frame_end(s);
  637. assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
  638. assert(s->current_picture.pict_type == s->pict_type);
  639. if (s->pict_type == B_TYPE || s->low_delay) {
  640. *pict= *(AVFrame*)s->current_picture_ptr;
  641. } else if (s->last_picture_ptr != NULL) {
  642. *pict= *(AVFrame*)s->last_picture_ptr;
  643. }
  644. if(s->last_picture_ptr || s->low_delay){
  645. *data_size = sizeof(AVFrame);
  646. ff_print_debug_info(s, pict);
  647. }
  648. /* Return the Picture timestamp as the frame number */
  649. /* we substract 1 because it is added on utils.c */
  650. avctx->frame_number = s->picture_number - 1;
  651. #ifdef PRINT_FRAME_TIME
  652. av_log(avctx, AV_LOG_DEBUG, "%Ld\n", rdtsc()-time);
  653. #endif
  654. return get_consumed_bytes(s, buf_size);
  655. }
  656. AVCodec mpeg4_decoder = {
  657. "mpeg4",
  658. CODEC_TYPE_VIDEO,
  659. CODEC_ID_MPEG4,
  660. sizeof(MpegEncContext),
  661. ff_h263_decode_init,
  662. NULL,
  663. ff_h263_decode_end,
  664. ff_h263_decode_frame,
  665. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  666. .flush= ff_mpeg_flush,
  667. };
  668. AVCodec h263_decoder = {
  669. "h263",
  670. CODEC_TYPE_VIDEO,
  671. CODEC_ID_H263,
  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 msmpeg4v1_decoder = {
  681. "msmpeg4v1",
  682. CODEC_TYPE_VIDEO,
  683. CODEC_ID_MSMPEG4V1,
  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,
  690. };
  691. AVCodec msmpeg4v2_decoder = {
  692. "msmpeg4v2",
  693. CODEC_TYPE_VIDEO,
  694. CODEC_ID_MSMPEG4V2,
  695. sizeof(MpegEncContext),
  696. ff_h263_decode_init,
  697. NULL,
  698. ff_h263_decode_end,
  699. ff_h263_decode_frame,
  700. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  701. };
  702. AVCodec msmpeg4v3_decoder = {
  703. "msmpeg4",
  704. CODEC_TYPE_VIDEO,
  705. CODEC_ID_MSMPEG4V3,
  706. sizeof(MpegEncContext),
  707. ff_h263_decode_init,
  708. NULL,
  709. ff_h263_decode_end,
  710. ff_h263_decode_frame,
  711. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  712. };
  713. AVCodec wmv1_decoder = {
  714. "wmv1",
  715. CODEC_TYPE_VIDEO,
  716. CODEC_ID_WMV1,
  717. sizeof(MpegEncContext),
  718. ff_h263_decode_init,
  719. NULL,
  720. ff_h263_decode_end,
  721. ff_h263_decode_frame,
  722. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  723. };
  724. AVCodec h263i_decoder = {
  725. "h263i",
  726. CODEC_TYPE_VIDEO,
  727. CODEC_ID_H263I,
  728. sizeof(MpegEncContext),
  729. ff_h263_decode_init,
  730. NULL,
  731. ff_h263_decode_end,
  732. ff_h263_decode_frame,
  733. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
  734. };
  735. AVCodec flv_decoder = {
  736. "flv",
  737. CODEC_TYPE_VIDEO,
  738. CODEC_ID_FLV1,
  739. sizeof(MpegEncContext),
  740. ff_h263_decode_init,
  741. NULL,
  742. ff_h263_decode_end,
  743. ff_h263_decode_frame,
  744. CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1
  745. };
  746. #ifdef CONFIG_H263_PARSER
  747. AVCodecParser h263_parser = {
  748. { CODEC_ID_H263 },
  749. sizeof(ParseContext),
  750. NULL,
  751. h263_parse,
  752. ff_parse_close,
  753. };
  754. #endif