h263dec.c 26 KB

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