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.

817 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
  24. * H.263 decoder.
  25. */
  26. #define UNCHECKED_BITSTREAM_READER 1
  27. #include "libavutil/cpu.h"
  28. #include "internal.h"
  29. #include "avcodec.h"
  30. #include "error_resilience.h"
  31. #include "mpegvideo.h"
  32. #include "h263.h"
  33. #include "h263_parser.h"
  34. #include "mpeg4video_parser.h"
  35. #include "msmpeg4.h"
  36. #include "vdpau_internal.h"
  37. #include "thread.h"
  38. #include "flv.h"
  39. #include "mpeg4video.h"
  40. av_cold int ff_h263_decode_init(AVCodecContext *avctx)
  41. {
  42. MpegEncContext *s = avctx->priv_data;
  43. int ret;
  44. s->avctx = avctx;
  45. s->out_format = FMT_H263;
  46. s->width = avctx->coded_width;
  47. s->height = avctx->coded_height;
  48. s->workaround_bugs= avctx->workaround_bugs;
  49. // set defaults
  50. ff_MPV_decode_defaults(s);
  51. s->quant_precision=5;
  52. s->decode_mb= ff_h263_decode_mb;
  53. s->low_delay= 1;
  54. if (avctx->codec->id == AV_CODEC_ID_MSS2)
  55. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  56. else
  57. avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
  58. s->unrestricted_mv= 1;
  59. /* select sub codec */
  60. switch(avctx->codec->id) {
  61. case AV_CODEC_ID_H263:
  62. case AV_CODEC_ID_H263P:
  63. s->unrestricted_mv= 0;
  64. avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
  65. break;
  66. case AV_CODEC_ID_MPEG4:
  67. break;
  68. case AV_CODEC_ID_MSMPEG4V1:
  69. s->h263_pred = 1;
  70. s->msmpeg4_version=1;
  71. break;
  72. case AV_CODEC_ID_MSMPEG4V2:
  73. s->h263_pred = 1;
  74. s->msmpeg4_version=2;
  75. break;
  76. case AV_CODEC_ID_MSMPEG4V3:
  77. s->h263_pred = 1;
  78. s->msmpeg4_version=3;
  79. break;
  80. case AV_CODEC_ID_WMV1:
  81. s->h263_pred = 1;
  82. s->msmpeg4_version=4;
  83. break;
  84. case AV_CODEC_ID_WMV2:
  85. s->h263_pred = 1;
  86. s->msmpeg4_version=5;
  87. break;
  88. case AV_CODEC_ID_VC1:
  89. case AV_CODEC_ID_WMV3:
  90. case AV_CODEC_ID_VC1IMAGE:
  91. case AV_CODEC_ID_WMV3IMAGE:
  92. case AV_CODEC_ID_MSS2:
  93. s->h263_pred = 1;
  94. s->msmpeg4_version=6;
  95. avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
  96. break;
  97. case AV_CODEC_ID_H263I:
  98. break;
  99. case AV_CODEC_ID_FLV1:
  100. s->h263_flv = 1;
  101. break;
  102. default:
  103. return AVERROR(EINVAL);
  104. }
  105. s->codec_id= avctx->codec->id;
  106. avctx->hwaccel= ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
  107. if (avctx->stream_codec_tag == AV_RL32("l263") && avctx->extradata_size == 56 && avctx->extradata[0] == 1)
  108. s->ehc_mode = 1;
  109. /* for h263, we allocate the images after having read the header */
  110. if (avctx->codec->id != AV_CODEC_ID_H263 && avctx->codec->id != AV_CODEC_ID_H263P && avctx->codec->id != AV_CODEC_ID_MPEG4)
  111. if ((ret = ff_MPV_common_init(s)) < 0)
  112. return ret;
  113. ff_h263_decode_init_vlc();
  114. return 0;
  115. }
  116. av_cold int ff_h263_decode_end(AVCodecContext *avctx)
  117. {
  118. MpegEncContext *s = avctx->priv_data;
  119. ff_MPV_common_end(s);
  120. return 0;
  121. }
  122. /**
  123. * Return 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 ? (ER_AC_END|ER_AC_ERROR) : 0x7F;
  142. const int mb_size= 16>>s->avctx->lowres;
  143. int ret;
  144. s->last_resync_gb= s->gb;
  145. s->first_slice_line= 1;
  146. s->resync_mb_x= s->mb_x;
  147. s->resync_mb_y= s->mb_y;
  148. ff_set_qscale(s, s->qscale);
  149. if (s->avctx->hwaccel) {
  150. const uint8_t *start= s->gb.buffer + get_bits_count(&s->gb)/8;
  151. ret = s->avctx->hwaccel->decode_slice(s->avctx, start, s->gb.buffer_end - start);
  152. // ensure we exit decode loop
  153. s->mb_y = s->mb_height;
  154. return ret;
  155. }
  156. if(s->partitioned_frame){
  157. const int qscale= s->qscale;
  158. if(CONFIG_MPEG4_DECODER && s->codec_id==AV_CODEC_ID_MPEG4){
  159. if ((ret = ff_mpeg4_decode_partitions(s)) < 0)
  160. return ret;
  161. }
  162. /* restore variables which were modified */
  163. s->first_slice_line=1;
  164. s->mb_x= s->resync_mb_x;
  165. s->mb_y= s->resync_mb_y;
  166. ff_set_qscale(s, qscale);
  167. }
  168. for(; s->mb_y < s->mb_height; s->mb_y++) {
  169. /* per-row end of slice checks */
  170. if(s->msmpeg4_version){
  171. if(s->resync_mb_y + s->slice_height == s->mb_y){
  172. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_END);
  173. return 0;
  174. }
  175. }
  176. if(s->msmpeg4_version==1){
  177. s->last_dc[0]=
  178. s->last_dc[1]=
  179. s->last_dc[2]= 128;
  180. }
  181. ff_init_block_index(s);
  182. for(; s->mb_x < s->mb_width; s->mb_x++) {
  183. int ret;
  184. ff_update_block_index(s);
  185. if(s->resync_mb_x == s->mb_x && s->resync_mb_y+1 == s->mb_y){
  186. s->first_slice_line=0;
  187. }
  188. /* DCT & quantize */
  189. s->mv_dir = MV_DIR_FORWARD;
  190. s->mv_type = MV_TYPE_16X16;
  191. // s->mb_skipped = 0;
  192. av_dlog(s, "%d %d %06X\n",
  193. ret, get_bits_count(&s->gb), show_bits(&s->gb, 24));
  194. ret= s->decode_mb(s, s->block);
  195. if (s->pict_type!=AV_PICTURE_TYPE_B)
  196. ff_h263_update_motion_val(s);
  197. if(ret<0){
  198. const int xy= s->mb_x + s->mb_y*s->mb_stride;
  199. if(ret==SLICE_END){
  200. ff_MPV_decode_mb(s, s->block);
  201. if(s->loop_filter)
  202. ff_h263_loop_filter(s);
  203. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, ER_MB_END&part_mask);
  204. s->padding_bug_score--;
  205. if(++s->mb_x >= s->mb_width){
  206. s->mb_x=0;
  207. ff_mpeg_draw_horiz_band(s, s->mb_y*mb_size, mb_size);
  208. ff_MPV_report_decode_progress(s);
  209. s->mb_y++;
  210. }
  211. return 0;
  212. }else if(ret==SLICE_NOEND){
  213. av_log(s->avctx, AV_LOG_ERROR, "Slice mismatch at MB: %d\n", xy);
  214. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x+1, s->mb_y, ER_MB_END&part_mask);
  215. return AVERROR_INVALIDDATA;
  216. }
  217. av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", xy);
  218. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR&part_mask);
  219. return AVERROR_INVALIDDATA;
  220. }
  221. ff_MPV_decode_mb(s, s->block);
  222. if(s->loop_filter)
  223. ff_h263_loop_filter(s);
  224. }
  225. ff_mpeg_draw_horiz_band(s, s->mb_y*mb_size, mb_size);
  226. ff_MPV_report_decode_progress(s);
  227. s->mb_x= 0;
  228. }
  229. av_assert1(s->mb_x==0 && s->mb_y==s->mb_height);
  230. if(s->codec_id==AV_CODEC_ID_MPEG4
  231. && (s->workaround_bugs&FF_BUG_AUTODETECT)
  232. && get_bits_left(&s->gb) >= 48
  233. && show_bits(&s->gb, 24)==0x4010
  234. && !s->data_partitioning)
  235. s->padding_bug_score+=32;
  236. /* try to detect the padding bug */
  237. if( s->codec_id==AV_CODEC_ID_MPEG4
  238. && (s->workaround_bugs&FF_BUG_AUTODETECT)
  239. && get_bits_left(&s->gb) >=0
  240. && get_bits_left(&s->gb) < 137
  241. // && !s->resync_marker
  242. && !s->data_partitioning){
  243. const int bits_count= get_bits_count(&s->gb);
  244. const int bits_left = s->gb.size_in_bits - bits_count;
  245. if(bits_left==0){
  246. s->padding_bug_score+=16;
  247. } else if(bits_left != 1){
  248. int v= show_bits(&s->gb, 8);
  249. v|= 0x7F >> (7-(bits_count&7));
  250. if(v==0x7F && bits_left<=8)
  251. s->padding_bug_score--;
  252. else if(v==0x7F && ((get_bits_count(&s->gb)+8)&8) && bits_left<=16)
  253. s->padding_bug_score+= 4;
  254. else
  255. s->padding_bug_score++;
  256. }
  257. }
  258. if(s->workaround_bugs&FF_BUG_AUTODETECT){
  259. if(s->padding_bug_score > -2 && !s->data_partitioning /*&& (s->divx_version>=0 || !s->resync_marker)*/)
  260. s->workaround_bugs |= FF_BUG_NO_PADDING;
  261. else
  262. s->workaround_bugs &= ~FF_BUG_NO_PADDING;
  263. }
  264. // handle formats which don't have unique end markers
  265. if(s->msmpeg4_version || (s->workaround_bugs&FF_BUG_NO_PADDING)){ //FIXME perhaps solve this more cleanly
  266. int left= get_bits_left(&s->gb);
  267. int max_extra=7;
  268. /* no markers in M$ crap */
  269. if(s->msmpeg4_version && s->pict_type==AV_PICTURE_TYPE_I)
  270. max_extra+= 17;
  271. /* buggy padding but the frame should still end approximately at the bitstream end */
  272. if((s->workaround_bugs&FF_BUG_NO_PADDING) && (s->err_recognition&(AV_EF_BUFFER|AV_EF_AGGRESSIVE)))
  273. max_extra+= 48;
  274. else if((s->workaround_bugs&FF_BUG_NO_PADDING))
  275. max_extra+= 256*256*256*64;
  276. if(left>max_extra){
  277. av_log(s->avctx, AV_LOG_ERROR, "discarding %d junk bits at end, next would be %X\n", left, show_bits(&s->gb, 24));
  278. }
  279. else if(left<0){
  280. av_log(s->avctx, AV_LOG_ERROR, "overreading %d bits\n", -left);
  281. }else
  282. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_END);
  283. return 0;
  284. }
  285. av_log(s->avctx, AV_LOG_ERROR, "slice end not reached but screenspace end (%d left %06X, score= %d)\n",
  286. get_bits_left(&s->gb),
  287. show_bits(&s->gb, 24), s->padding_bug_score);
  288. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, ER_MB_END&part_mask);
  289. return AVERROR_INVALIDDATA;
  290. }
  291. int ff_h263_decode_frame(AVCodecContext *avctx,
  292. void *data, int *got_frame,
  293. AVPacket *avpkt)
  294. {
  295. const uint8_t *buf = avpkt->data;
  296. int buf_size = avpkt->size;
  297. MpegEncContext *s = avctx->priv_data;
  298. int ret;
  299. AVFrame *pict = data;
  300. s->flags= avctx->flags;
  301. s->flags2= avctx->flags2;
  302. /* no supplementary picture */
  303. if (buf_size == 0) {
  304. /* special case for last picture */
  305. if (s->low_delay==0 && s->next_picture_ptr) {
  306. if ((ret = av_frame_ref(pict, &s->next_picture_ptr->f)) < 0)
  307. return ret;
  308. s->next_picture_ptr= NULL;
  309. *got_frame = 1;
  310. }
  311. return 0;
  312. }
  313. if(s->flags&CODEC_FLAG_TRUNCATED){
  314. int next;
  315. if(CONFIG_MPEG4_DECODER && s->codec_id==AV_CODEC_ID_MPEG4){
  316. next= ff_mpeg4_find_frame_end(&s->parse_context, buf, buf_size);
  317. }else if(CONFIG_H263_DECODER && s->codec_id==AV_CODEC_ID_H263){
  318. next= ff_h263_find_frame_end(&s->parse_context, buf, buf_size);
  319. }else if(CONFIG_H263P_DECODER && s->codec_id==AV_CODEC_ID_H263P){
  320. next= ff_h263_find_frame_end(&s->parse_context, buf, buf_size);
  321. }else{
  322. av_log(s->avctx, AV_LOG_ERROR, "this codec does not support truncated bitstreams\n");
  323. return AVERROR(EINVAL);
  324. }
  325. if( ff_combine_frame(&s->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0 )
  326. return buf_size;
  327. }
  328. retry:
  329. if(s->divx_packed && s->bitstream_buffer_size){
  330. int i;
  331. for(i=0; i<buf_size-3; i++){
  332. if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==1){
  333. if(buf[i+3]==0xB0){
  334. av_log(s->avctx, AV_LOG_WARNING, "Discarding excessive bitstream in packed xvid\n");
  335. s->bitstream_buffer_size=0;
  336. }
  337. break;
  338. }
  339. }
  340. }
  341. if(s->bitstream_buffer_size && (s->divx_packed || buf_size<20)){ //divx 5.01+/xvid frame reorder
  342. init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size*8);
  343. }else
  344. init_get_bits(&s->gb, buf, buf_size*8);
  345. s->bitstream_buffer_size=0;
  346. if (!s->context_initialized) {
  347. if ((ret = ff_MPV_common_init(s)) < 0) //we need the idct permutaton for reading a custom matrix
  348. return ret;
  349. }
  350. /* We need to set current_picture_ptr before reading the header,
  351. * otherwise we cannot store anyting in there */
  352. if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
  353. int i= ff_find_unused_picture(s, 0);
  354. if (i < 0)
  355. return i;
  356. s->current_picture_ptr= &s->picture[i];
  357. }
  358. /* let's go :-) */
  359. if (CONFIG_WMV2_DECODER && s->msmpeg4_version==5) {
  360. ret= ff_wmv2_decode_picture_header(s);
  361. } else if (CONFIG_MSMPEG4_DECODER && s->msmpeg4_version) {
  362. ret = ff_msmpeg4_decode_picture_header(s);
  363. } else if (CONFIG_MPEG4_DECODER && s->h263_pred) {
  364. if(s->avctx->extradata_size && s->picture_number==0){
  365. GetBitContext gb;
  366. init_get_bits(&gb, s->avctx->extradata, s->avctx->extradata_size*8);
  367. ret = ff_mpeg4_decode_picture_header(s, &gb);
  368. }
  369. ret = ff_mpeg4_decode_picture_header(s, &s->gb);
  370. } else if (CONFIG_H263I_DECODER && s->codec_id == AV_CODEC_ID_H263I) {
  371. ret = ff_intel_h263_decode_picture_header(s);
  372. } else if (CONFIG_FLV_DECODER && s->h263_flv) {
  373. ret = ff_flv_decode_picture_header(s);
  374. } else {
  375. ret = ff_h263_decode_picture_header(s);
  376. }
  377. if (ret < 0 || ret==FRAME_SKIPPED) {
  378. if ( s->width != avctx->coded_width
  379. || s->height != avctx->coded_height) {
  380. av_log(s->avctx, AV_LOG_WARNING, "Reverting picture dimensions change due to header decoding failure\n");
  381. s->width = avctx->coded_width;
  382. s->height= avctx->coded_height;
  383. }
  384. }
  385. if(ret==FRAME_SKIPPED) return get_consumed_bytes(s, buf_size);
  386. /* skip if the header was thrashed */
  387. if (ret < 0){
  388. av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
  389. return ret;
  390. }
  391. avctx->has_b_frames= !s->low_delay;
  392. if(s->xvid_build==-1 && s->divx_version==-1 && s->lavc_build==-1){
  393. if(s->stream_codec_tag == AV_RL32("XVID") ||
  394. s->codec_tag == AV_RL32("XVID") || s->codec_tag == AV_RL32("XVIX") ||
  395. s->codec_tag == AV_RL32("RMP4") || s->codec_tag == AV_RL32("ZMP4") ||
  396. s->codec_tag == AV_RL32("SIPP")
  397. )
  398. s->xvid_build= 0;
  399. #if 0
  400. if(s->codec_tag == AV_RL32("DIVX") && s->vo_type==0 && s->vol_control_parameters==1
  401. && s->padding_bug_score > 0 && s->low_delay) // XVID with modified fourcc
  402. s->xvid_build= 0;
  403. #endif
  404. }
  405. if(s->xvid_build==-1 && s->divx_version==-1 && s->lavc_build==-1){
  406. if(s->codec_tag == AV_RL32("DIVX") && s->vo_type==0 && s->vol_control_parameters==0)
  407. s->divx_version= 400; //divx 4
  408. }
  409. if(s->xvid_build>=0 && s->divx_version>=0){
  410. s->divx_version=
  411. s->divx_build= -1;
  412. }
  413. if(s->workaround_bugs&FF_BUG_AUTODETECT){
  414. if(s->codec_tag == AV_RL32("XVIX"))
  415. s->workaround_bugs|= FF_BUG_XVID_ILACE;
  416. if(s->codec_tag == AV_RL32("UMP4")){
  417. s->workaround_bugs|= FF_BUG_UMP4;
  418. }
  419. if(s->divx_version>=500 && s->divx_build<1814){
  420. s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
  421. }
  422. if(s->divx_version>502 && s->divx_build<1814){
  423. s->workaround_bugs|= FF_BUG_QPEL_CHROMA2;
  424. }
  425. if(s->xvid_build<=3U)
  426. s->padding_bug_score= 256*256*256*64;
  427. if(s->xvid_build<=1U)
  428. s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
  429. if(s->xvid_build<=12U)
  430. s->workaround_bugs|= FF_BUG_EDGE;
  431. if(s->xvid_build<=32U)
  432. s->workaround_bugs|= FF_BUG_DC_CLIP;
  433. #define SET_QPEL_FUNC(postfix1, postfix2) \
  434. s->dsp.put_ ## postfix1 = ff_put_ ## postfix2;\
  435. s->dsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2;\
  436. s->dsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
  437. if(s->lavc_build<4653U)
  438. s->workaround_bugs|= FF_BUG_STD_QPEL;
  439. if(s->lavc_build<4655U)
  440. s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
  441. if(s->lavc_build<4670U){
  442. s->workaround_bugs|= FF_BUG_EDGE;
  443. }
  444. if(s->lavc_build<=4712U)
  445. s->workaround_bugs|= FF_BUG_DC_CLIP;
  446. if(s->divx_version>=0)
  447. s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
  448. if(s->divx_version==501 && s->divx_build==20020416)
  449. s->padding_bug_score= 256*256*256*64;
  450. if(s->divx_version<500U){
  451. s->workaround_bugs|= FF_BUG_EDGE;
  452. }
  453. if(s->divx_version>=0)
  454. s->workaround_bugs|= FF_BUG_HPEL_CHROMA;
  455. #if 0
  456. if(s->divx_version==500)
  457. s->padding_bug_score= 256*256*256*64;
  458. /* very ugly XVID padding bug detection FIXME/XXX solve this differently
  459. * Let us hope this at least works.
  460. */
  461. if( s->resync_marker==0 && s->data_partitioning==0 && s->divx_version==-1
  462. && s->codec_id==AV_CODEC_ID_MPEG4 && s->vo_type==0)
  463. s->workaround_bugs|= FF_BUG_NO_PADDING;
  464. if(s->lavc_build<4609U) //FIXME not sure about the version num but a 4609 file seems ok
  465. s->workaround_bugs|= FF_BUG_NO_PADDING;
  466. #endif
  467. }
  468. if(s->workaround_bugs& FF_BUG_STD_QPEL){
  469. SET_QPEL_FUNC(qpel_pixels_tab[0][ 5], qpel16_mc11_old_c)
  470. SET_QPEL_FUNC(qpel_pixels_tab[0][ 7], qpel16_mc31_old_c)
  471. SET_QPEL_FUNC(qpel_pixels_tab[0][ 9], qpel16_mc12_old_c)
  472. SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
  473. SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
  474. SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
  475. SET_QPEL_FUNC(qpel_pixels_tab[1][ 5], qpel8_mc11_old_c)
  476. SET_QPEL_FUNC(qpel_pixels_tab[1][ 7], qpel8_mc31_old_c)
  477. SET_QPEL_FUNC(qpel_pixels_tab[1][ 9], qpel8_mc12_old_c)
  478. SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
  479. SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
  480. SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
  481. }
  482. if(avctx->debug & FF_DEBUG_BUGS)
  483. av_log(s->avctx, AV_LOG_DEBUG, "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
  484. s->workaround_bugs, s->lavc_build, s->xvid_build, s->divx_version, s->divx_build,
  485. s->divx_packed ? "p" : "");
  486. #if HAVE_MMX
  487. if (s->codec_id == AV_CODEC_ID_MPEG4 && s->xvid_build>=0 && avctx->idct_algo == FF_IDCT_AUTO && (av_get_cpu_flags() & AV_CPU_FLAG_MMX)) {
  488. avctx->idct_algo= FF_IDCT_XVIDMMX;
  489. ff_dct_common_init(s);
  490. goto retry;
  491. }
  492. #endif
  493. /* After H263 & mpeg4 header decode we have the height, width,*/
  494. /* and other parameters. So then we could init the picture */
  495. /* FIXME: By the way H263 decoder is evolving it should have */
  496. /* an H263EncContext */
  497. if (s->width != avctx->coded_width ||
  498. s->height != avctx->coded_height ||
  499. s->context_reinit) {
  500. /* H.263 could change picture size any time */
  501. s->context_reinit = 0;
  502. avcodec_set_dimensions(avctx, s->width, s->height);
  503. if ((ret = ff_MPV_common_frame_size_change(s)))
  504. return ret;
  505. }
  506. if((s->codec_id==AV_CODEC_ID_H263 || s->codec_id==AV_CODEC_ID_H263P || s->codec_id == AV_CODEC_ID_H263I))
  507. s->gob_index = ff_h263_get_gob_height(s);
  508. // for skipping the frame
  509. s->current_picture.f.pict_type = s->pict_type;
  510. s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  511. /* skip B-frames if we don't have reference frames */
  512. if (s->last_picture_ptr == NULL &&
  513. (s->pict_type == AV_PICTURE_TYPE_B || s->droppable))
  514. return get_consumed_bytes(s, buf_size);
  515. if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==AV_PICTURE_TYPE_B)
  516. || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=AV_PICTURE_TYPE_I)
  517. || avctx->skip_frame >= AVDISCARD_ALL)
  518. return get_consumed_bytes(s, buf_size);
  519. if(s->next_p_frame_damaged){
  520. if(s->pict_type==AV_PICTURE_TYPE_B)
  521. return get_consumed_bytes(s, buf_size);
  522. else
  523. s->next_p_frame_damaged=0;
  524. }
  525. if((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
  526. s->me.qpel_put= s->dsp.put_qpel_pixels_tab;
  527. s->me.qpel_avg= s->dsp.avg_qpel_pixels_tab;
  528. }else{
  529. s->me.qpel_put= s->dsp.put_no_rnd_qpel_pixels_tab;
  530. s->me.qpel_avg= s->dsp.avg_qpel_pixels_tab;
  531. }
  532. if ((ret = ff_MPV_frame_start(s, avctx)) < 0)
  533. return ret;
  534. if (!s->divx_packed && !avctx->hwaccel)
  535. ff_thread_finish_setup(avctx);
  536. if (CONFIG_MPEG4_VDPAU_DECODER && (s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)) {
  537. ff_vdpau_mpeg4_decode_picture(s, s->gb.buffer, s->gb.buffer_end - s->gb.buffer);
  538. goto frame_end;
  539. }
  540. if (avctx->hwaccel) {
  541. if ((ret = avctx->hwaccel->start_frame(avctx, s->gb.buffer, s->gb.buffer_end - s->gb.buffer)) < 0)
  542. return ret;
  543. }
  544. ff_mpeg_er_frame_start(s);
  545. //the second part of the wmv2 header contains the MB skip bits which are stored in current_picture->mb_type
  546. //which is not available before ff_MPV_frame_start()
  547. if (CONFIG_WMV2_DECODER && s->msmpeg4_version==5){
  548. ret = ff_wmv2_decode_secondary_picture_header(s);
  549. if(ret<0) return ret;
  550. if(ret==1) goto frame_end;
  551. }
  552. /* decode each macroblock */
  553. s->mb_x=0;
  554. s->mb_y=0;
  555. ret = decode_slice(s);
  556. while(s->mb_y<s->mb_height){
  557. if(s->msmpeg4_version){
  558. if(s->slice_height==0 || s->mb_x!=0 || (s->mb_y%s->slice_height)!=0 || get_bits_left(&s->gb)<0)
  559. break;
  560. }else{
  561. int prev_x=s->mb_x, prev_y=s->mb_y;
  562. if(ff_h263_resync(s)<0)
  563. break;
  564. if (prev_y * s->mb_width + prev_x < s->mb_y * s->mb_width + s->mb_x)
  565. s->er.error_occurred = 1;
  566. }
  567. if(s->msmpeg4_version<4 && s->h263_pred)
  568. ff_mpeg4_clean_buffers(s);
  569. if (decode_slice(s) < 0) ret = AVERROR_INVALIDDATA;
  570. }
  571. if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type==AV_PICTURE_TYPE_I)
  572. if(!CONFIG_MSMPEG4_DECODER || ff_msmpeg4_decode_ext_header(s, buf_size) < 0){
  573. s->er.error_status_table[s->mb_num - 1] = ER_MB_ERROR;
  574. }
  575. av_assert1(s->bitstream_buffer_size==0);
  576. frame_end:
  577. ff_er_frame_end(&s->er);
  578. if (avctx->hwaccel) {
  579. if ((ret = avctx->hwaccel->end_frame(avctx)) < 0)
  580. return ret;
  581. }
  582. ff_MPV_frame_end(s);
  583. /* divx 5.01+ bitstream reorder stuff */
  584. /* Since this clobbers the input buffer and hwaccel codecs still need the
  585. * data during hwaccel->end_frame we should not do this any earlier */
  586. if(s->codec_id==AV_CODEC_ID_MPEG4 && s->divx_packed){
  587. int current_pos= s->gb.buffer == s->bitstream_buffer ? 0 : (get_bits_count(&s->gb)>>3);
  588. int startcode_found=0;
  589. if(buf_size - current_pos > 7){
  590. int i;
  591. for(i=current_pos; i<buf_size-4; i++){
  592. if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==1 && buf[i+3]==0xB6){
  593. startcode_found=!(buf[i+4]&0x40);
  594. break;
  595. }
  596. }
  597. }
  598. if(startcode_found){
  599. av_fast_malloc(
  600. &s->bitstream_buffer,
  601. &s->allocated_bitstream_buffer_size,
  602. buf_size - current_pos + FF_INPUT_BUFFER_PADDING_SIZE);
  603. if (!s->bitstream_buffer)
  604. return AVERROR(ENOMEM);
  605. memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
  606. s->bitstream_buffer_size= buf_size - current_pos;
  607. }
  608. }
  609. if (!s->divx_packed && avctx->hwaccel)
  610. ff_thread_finish_setup(avctx);
  611. av_assert1(s->current_picture.f.pict_type == s->current_picture_ptr->f.pict_type);
  612. av_assert1(s->current_picture.f.pict_type == s->pict_type);
  613. if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
  614. if ((ret = av_frame_ref(pict, &s->current_picture_ptr->f)) < 0)
  615. return ret;
  616. ff_print_debug_info(s, s->current_picture_ptr, pict);
  617. ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, FF_QSCALE_TYPE_MPEG1);
  618. } else if (s->last_picture_ptr != NULL) {
  619. if ((ret = av_frame_ref(pict, &s->last_picture_ptr->f)) < 0)
  620. return ret;
  621. ff_print_debug_info(s, s->last_picture_ptr, pict);
  622. ff_mpv_export_qp_table(s, pict, s->last_picture_ptr, FF_QSCALE_TYPE_MPEG1);
  623. }
  624. if(s->last_picture_ptr || s->low_delay){
  625. if ( pict->format == AV_PIX_FMT_YUV420P
  626. && (s->codec_tag == AV_RL32("GEOV") || s->codec_tag == AV_RL32("GEOX"))) {
  627. int x, y, p;
  628. av_frame_make_writable(pict);
  629. for (p=0; p<3; p++) {
  630. int w = FF_CEIL_RSHIFT(pict-> width, !!p);
  631. int h = FF_CEIL_RSHIFT(pict->height, !!p);
  632. int linesize = pict->linesize[p];
  633. for (y=0; y<(h>>1); y++)
  634. for (x=0; x<w; x++)
  635. FFSWAP(int,
  636. pict->data[p][x + y*linesize],
  637. pict->data[p][x + (h-1-y)*linesize]);
  638. }
  639. }
  640. *got_frame = 1;
  641. }
  642. return (ret && (avctx->err_recognition & AV_EF_EXPLODE))?ret:get_consumed_bytes(s, buf_size);
  643. }
  644. const enum AVPixelFormat ff_h263_hwaccel_pixfmt_list_420[] = {
  645. #if CONFIG_VAAPI
  646. AV_PIX_FMT_VAAPI_VLD,
  647. #endif
  648. #if CONFIG_VDPAU
  649. AV_PIX_FMT_VDPAU,
  650. #endif
  651. AV_PIX_FMT_YUV420P,
  652. AV_PIX_FMT_NONE
  653. };
  654. AVCodec ff_h263_decoder = {
  655. .name = "h263",
  656. .long_name = NULL_IF_CONFIG_SMALL("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
  657. .type = AVMEDIA_TYPE_VIDEO,
  658. .id = AV_CODEC_ID_H263,
  659. .priv_data_size = sizeof(MpegEncContext),
  660. .init = ff_h263_decode_init,
  661. .close = ff_h263_decode_end,
  662. .decode = ff_h263_decode_frame,
  663. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
  664. CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  665. .flush = ff_mpeg_flush,
  666. .max_lowres = 3,
  667. .pix_fmts = ff_h263_hwaccel_pixfmt_list_420,
  668. };
  669. AVCodec ff_h263p_decoder = {
  670. .name = "h263p",
  671. .long_name = NULL_IF_CONFIG_SMALL("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
  672. .type = AVMEDIA_TYPE_VIDEO,
  673. .id = AV_CODEC_ID_H263P,
  674. .priv_data_size = sizeof(MpegEncContext),
  675. .init = ff_h263_decode_init,
  676. .close = ff_h263_decode_end,
  677. .decode = ff_h263_decode_frame,
  678. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
  679. CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
  680. .flush = ff_mpeg_flush,
  681. .max_lowres = 3,
  682. .pix_fmts = ff_h263_hwaccel_pixfmt_list_420,
  683. };