Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(506)

Side by Side Diff: media/filters/ffmpeg_video_decoder.cc

Issue 270193002: Replicate FFmpeg's video frame allocation strategy. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Comments. Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « media/ffmpeg/ffmpeg_regression_tests.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 239 matching lines...) Expand 10 before | Expand all | Expand 10 after
355 if (!codec || avcodec_open2(codec_context_.get(), codec, NULL) < 0) { 365 if (!codec || avcodec_open2(codec_context_.get(), codec, NULL) < 0) {
356 ReleaseFFmpegResources(); 366 ReleaseFFmpegResources();
357 return false; 367 return false;
358 } 368 }
359 369
360 av_frame_.reset(av_frame_alloc()); 370 av_frame_.reset(av_frame_alloc());
361 return true; 371 return true;
362 } 372 }
363 373
364 } // namespace media 374 } // namespace media
OLDNEW
« no previous file with comments | « media/ffmpeg/ffmpeg_regression_tests.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698