OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/filters/ffmpeg_video_decoder.h" | 5 #include "media/filters/ffmpeg_video_decoder.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 // whereas |codec_context| contains the current threads's | 65 // whereas |codec_context| contains the current threads's |
66 // updated width/height/pix_fmt, which can change for adaptive | 66 // updated width/height/pix_fmt, which can change for adaptive |
67 // content. | 67 // content. |
68 VideoFrame::Format format = PixelFormatToVideoFormat(codec_context->pix_fmt); | 68 VideoFrame::Format format = PixelFormatToVideoFormat(codec_context->pix_fmt); |
69 if (format == VideoFrame::UNKNOWN) | 69 if (format == VideoFrame::UNKNOWN) |
70 return AVERROR(EINVAL); | 70 return AVERROR(EINVAL); |
71 DCHECK(format == VideoFrame::YV12 || format == VideoFrame::YV16 || | 71 DCHECK(format == VideoFrame::YV12 || format == VideoFrame::YV16 || |
72 format == VideoFrame::YV12J); | 72 format == VideoFrame::YV12J); |
73 | 73 |
74 gfx::Size size(codec_context->width, codec_context->height); | 74 gfx::Size size(codec_context->width, codec_context->height); |
75 int ret; | 75 const int ret = av_image_check_size(size.width(), size.height(), 0, NULL); |
76 if ((ret = av_image_check_size(size.width(), size.height(), 0, NULL)) < 0) | 76 if (ret < 0) |
77 return ret; | 77 return ret; |
78 | 78 |
79 gfx::Size natural_size; | 79 gfx::Size natural_size; |
80 if (codec_context->sample_aspect_ratio.num > 0) { | 80 if (codec_context->sample_aspect_ratio.num > 0) { |
81 natural_size = GetNaturalSize(size, | 81 natural_size = GetNaturalSize(size, |
82 codec_context->sample_aspect_ratio.num, | 82 codec_context->sample_aspect_ratio.num, |
83 codec_context->sample_aspect_ratio.den); | 83 codec_context->sample_aspect_ratio.den); |
84 } else { | 84 } else { |
85 natural_size = config_.natural_size(); | 85 natural_size = config_.natural_size(); |
86 } | 86 } |
87 | 87 |
88 if (!VideoFrame::IsValidConfig(format, size, gfx::Rect(size), natural_size)) | 88 // FFmpeg has specific requirements on the allocation size of the frame. The |
| 89 // following logic replicates FFmpeg's allocation strategy to ensure buffers |
| 90 // are not overread / overwritten. See ff_init_buffer_info() for details. |
| 91 // |
| 92 // When lowres is non-zero, dimensions should be divided by 2^(lowres), but |
| 93 // since we don't use this, just DCHECK that it's zero. |
| 94 DCHECK_EQ(codec_context->lowres, 0); |
| 95 gfx::Size coded_size(std::max(size.width(), codec_context->coded_width), |
| 96 std::max(size.height(), codec_context->coded_height)); |
| 97 |
| 98 if (!VideoFrame::IsValidConfig( |
| 99 format, coded_size, gfx::Rect(size), natural_size)) |
89 return AVERROR(EINVAL); | 100 return AVERROR(EINVAL); |
90 | 101 |
91 scoped_refptr<VideoFrame> video_frame = | 102 scoped_refptr<VideoFrame> video_frame = frame_pool_.CreateFrame( |
92 frame_pool_.CreateFrame(format, size, gfx::Rect(size), | 103 format, coded_size, gfx::Rect(size), natural_size, kNoTimestamp()); |
93 natural_size, kNoTimestamp()); | |
94 | 104 |
95 for (int i = 0; i < 3; i++) { | 105 for (int i = 0; i < 3; i++) { |
96 frame->base[i] = video_frame->data(i); | 106 frame->base[i] = video_frame->data(i); |
97 frame->data[i] = video_frame->data(i); | 107 frame->data[i] = video_frame->data(i); |
98 frame->linesize[i] = video_frame->stride(i); | 108 frame->linesize[i] = video_frame->stride(i); |
99 } | 109 } |
100 | 110 |
101 frame->opaque = NULL; | 111 frame->opaque = NULL; |
102 video_frame.swap(reinterpret_cast<VideoFrame**>(&frame->opaque)); | 112 video_frame.swap(reinterpret_cast<VideoFrame**>(&frame->opaque)); |
103 frame->type = FF_BUFFER_TYPE_USER; | 113 frame->type = FF_BUFFER_TYPE_USER; |
104 frame->width = codec_context->width; | 114 frame->width = coded_size.width(); |
105 frame->height = codec_context->height; | 115 frame->height = coded_size.height(); |
106 frame->format = codec_context->pix_fmt; | 116 frame->format = codec_context->pix_fmt; |
107 | 117 |
108 return 0; | 118 return 0; |
109 } | 119 } |
110 | 120 |
111 static int GetVideoBufferImpl(AVCodecContext* s, AVFrame* frame) { | 121 static int GetVideoBufferImpl(AVCodecContext* s, AVFrame* frame) { |
112 FFmpegVideoDecoder* decoder = static_cast<FFmpegVideoDecoder*>(s->opaque); | 122 FFmpegVideoDecoder* decoder = static_cast<FFmpegVideoDecoder*>(s->opaque); |
113 return decoder->GetVideoBuffer(s, frame); | 123 return decoder->GetVideoBuffer(s, frame); |
114 } | 124 } |
115 | 125 |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 if (!codec || avcodec_open2(codec_context_.get(), codec, NULL) < 0) { | 384 if (!codec || avcodec_open2(codec_context_.get(), codec, NULL) < 0) { |
375 ReleaseFFmpegResources(); | 385 ReleaseFFmpegResources(); |
376 return false; | 386 return false; |
377 } | 387 } |
378 | 388 |
379 av_frame_.reset(av_frame_alloc()); | 389 av_frame_.reset(av_frame_alloc()); |
380 return true; | 390 return true; |
381 } | 391 } |
382 | 392 |
383 } // namespace media | 393 } // namespace media |
OLD | NEW |