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.

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