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.

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