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
3 // found in the LICENSE file.
5 #include "content/renderer/pepper/ppb_video_decoder_impl.h"
9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/metrics/histogram.h"
12 #include "content/common/gpu/client/gpu_channel_host.h"
13 #include "content/renderer/pepper/common.h"
14 #include "content/renderer/pepper/host_globals.h"
15 #include "content/renderer/pepper/pepper_plugin_instance_impl.h"
16 #include "content/renderer/pepper/plugin_module.h"
17 #include "content/renderer/pepper/ppb_buffer_impl.h"
18 #include "content/renderer/pepper/ppb_graphics_3d_impl.h"
19 #include "content/renderer/render_thread_impl.h"
20 #include "gpu/command_buffer/client/gles2_implementation.h"
21 #include "media/video/picture.h"
22 #include "media/video/video_decode_accelerator.h"
23 #include "ppapi/c/dev/pp_video_dev.h"
24 #include "ppapi/c/dev/ppb_video_decoder_dev.h"
25 #include "ppapi/c/dev/ppp_video_decoder_dev.h"
26 #include "ppapi/c/pp_completion_callback.h"
27 #include "ppapi/c/pp_errors.h"
28 #include "ppapi/shared_impl/resource_tracker.h"
29 #include "ppapi/thunk/enter.h"
31 using ppapi::TrackedCallback;
32 using ppapi::thunk::EnterResourceNoLock;
33 using ppapi::thunk::PPB_Buffer_API;
34 using ppapi::thunk::PPB_Graphics3D_API;
38 // Convert PP_VideoDecoder_Profile to media::VideoCodecProfile.
39 media::VideoCodecProfile PPToMediaProfile(
40 const PP_VideoDecoder_Profile pp_profile) {
42 case PP_VIDEODECODER_H264PROFILE_NONE:
43 // HACK: PPAPI contains a bogus "none" h264 profile that doesn't
44 // correspond to anything in h.264; but a number of released chromium
45 // versions silently promoted this to Baseline profile, so we retain that
46 // behavior here. Fall through.
47 case PP_VIDEODECODER_H264PROFILE_BASELINE:
48 return media::H264PROFILE_BASELINE;
49 case PP_VIDEODECODER_H264PROFILE_MAIN:
50 return media::H264PROFILE_MAIN;
51 case PP_VIDEODECODER_H264PROFILE_EXTENDED:
52 return media::H264PROFILE_EXTENDED;
53 case PP_VIDEODECODER_H264PROFILE_HIGH:
54 return media::H264PROFILE_HIGH;
55 case PP_VIDEODECODER_H264PROFILE_HIGH10PROFILE:
56 return media::H264PROFILE_HIGH10PROFILE;
57 case PP_VIDEODECODER_H264PROFILE_HIGH422PROFILE:
58 return media::H264PROFILE_HIGH422PROFILE;
59 case PP_VIDEODECODER_H264PROFILE_HIGH444PREDICTIVEPROFILE:
60 return media::H264PROFILE_HIGH444PREDICTIVEPROFILE;
61 case PP_VIDEODECODER_H264PROFILE_SCALABLEBASELINE:
62 return media::H264PROFILE_SCALABLEBASELINE;
63 case PP_VIDEODECODER_H264PROFILE_SCALABLEHIGH:
64 return media::H264PROFILE_SCALABLEHIGH;
65 case PP_VIDEODECODER_H264PROFILE_STEREOHIGH:
66 return media::H264PROFILE_STEREOHIGH;
67 case PP_VIDEODECODER_H264PROFILE_MULTIVIEWHIGH:
68 return media::H264PROFILE_MULTIVIEWHIGH;
69 case PP_VIDEODECODER_VP8PROFILE_MAIN:
70 return media::VP8PROFILE_MAIN;
72 return media::VIDEO_CODEC_PROFILE_UNKNOWN;
76 PP_VideoDecodeError_Dev MediaToPPError(
77 media::VideoDecodeAccelerator::Error error) {
79 case media::VideoDecodeAccelerator::ILLEGAL_STATE:
80 return PP_VIDEODECODERERROR_ILLEGAL_STATE;
81 case media::VideoDecodeAccelerator::INVALID_ARGUMENT:
82 return PP_VIDEODECODERERROR_INVALID_ARGUMENT;
83 case media::VideoDecodeAccelerator::UNREADABLE_INPUT:
84 return PP_VIDEODECODERERROR_UNREADABLE_INPUT;
85 case media::VideoDecodeAccelerator::PLATFORM_FAILURE:
86 return PP_VIDEODECODERERROR_PLATFORM_FAILURE;
89 return PP_VIDEODECODERERROR_ILLEGAL_STATE;
97 PPB_VideoDecoder_Impl::PPB_VideoDecoder_Impl(PP_Instance instance)
98 : PPB_VideoDecoder_Shared(instance), ppp_videodecoder_(NULL) {
99 PluginModule* plugin_module =
100 HostGlobals::Get()->GetInstance(pp_instance())->module();
102 ppp_videodecoder_ = static_cast<const PPP_VideoDecoder_Dev*>(
103 plugin_module->GetPluginInterface(PPP_VIDEODECODER_DEV_INTERFACE));
107 PPB_VideoDecoder_Impl::~PPB_VideoDecoder_Impl() { Destroy(); }
110 PP_Resource PPB_VideoDecoder_Impl::Create(PP_Instance instance,
111 PP_Resource graphics_context,
112 PP_VideoDecoder_Profile profile) {
113 scoped_refptr<PPB_VideoDecoder_Impl> decoder(
114 new PPB_VideoDecoder_Impl(instance));
115 if (decoder->Init(graphics_context, profile))
116 return decoder->GetReference();
120 bool PPB_VideoDecoder_Impl::Init(PP_Resource graphics_context,
121 PP_VideoDecoder_Profile profile) {
122 EnterResourceNoLock<PPB_Graphics3D_API> enter_context(graphics_context, true);
123 if (enter_context.failed())
126 PPB_Graphics3D_Impl* graphics_3d =
127 static_cast<PPB_Graphics3D_Impl*>(enter_context.object());
129 int command_buffer_route_id = graphics_3d->GetCommandBufferRouteId();
130 if (command_buffer_route_id == 0)
133 InitCommon(graphics_context, graphics_3d->gles2_impl());
134 FlushCommandBuffer();
136 // This is not synchronous, but subsequent IPC messages will be buffered, so
137 // it is okay to immediately send IPC messages through the returned channel.
138 GpuChannelHost* channel = graphics_3d->channel();
140 decoder_ = channel->CreateVideoDecoder(command_buffer_route_id);
141 return (decoder_ && decoder_->Initialize(PPToMediaProfile(profile), this));
144 int32_t PPB_VideoDecoder_Impl::Decode(
145 const PP_VideoBitstreamBuffer_Dev* bitstream_buffer,
146 scoped_refptr<TrackedCallback> callback) {
148 return PP_ERROR_BADRESOURCE;
150 EnterResourceNoLock<PPB_Buffer_API> enter(bitstream_buffer->data, true);
152 return PP_ERROR_FAILED;
154 PPB_Buffer_Impl* buffer = static_cast<PPB_Buffer_Impl*>(enter.object());
155 DCHECK_GE(bitstream_buffer->id, 0);
156 media::BitstreamBuffer decode_buffer(bitstream_buffer->id,
157 buffer->shared_memory()->handle(),
158 bitstream_buffer->size);
159 if (!SetBitstreamBufferCallback(bitstream_buffer->id, callback))
160 return PP_ERROR_BADARGUMENT;
162 FlushCommandBuffer();
163 decoder_->Decode(decode_buffer);
164 return PP_OK_COMPLETIONPENDING;
167 void PPB_VideoDecoder_Impl::AssignPictureBuffers(
168 uint32_t no_of_buffers,
169 const PP_PictureBuffer_Dev* buffers) {
172 UMA_HISTOGRAM_COUNTS_100("Media.PepperVideoDecoderPictureCount",
175 std::vector<media::PictureBuffer> wrapped_buffers;
176 for (uint32 i = 0; i < no_of_buffers; i++) {
177 PP_PictureBuffer_Dev in_buf = buffers[i];
178 DCHECK_GE(in_buf.id, 0);
179 media::PictureBuffer buffer(
181 gfx::Size(in_buf.size.width, in_buf.size.height),
183 wrapped_buffers.push_back(buffer);
184 UMA_HISTOGRAM_COUNTS_10000("Media.PepperVideoDecoderPictureHeight",
188 FlushCommandBuffer();
189 decoder_->AssignPictureBuffers(wrapped_buffers);
192 void PPB_VideoDecoder_Impl::ReusePictureBuffer(int32_t picture_buffer_id) {
196 FlushCommandBuffer();
197 decoder_->ReusePictureBuffer(picture_buffer_id);
200 int32_t PPB_VideoDecoder_Impl::Flush(scoped_refptr<TrackedCallback> callback) {
202 return PP_ERROR_BADRESOURCE;
204 if (!SetFlushCallback(callback))
205 return PP_ERROR_INPROGRESS;
207 FlushCommandBuffer();
209 return PP_OK_COMPLETIONPENDING;
212 int32_t PPB_VideoDecoder_Impl::Reset(scoped_refptr<TrackedCallback> callback) {
214 return PP_ERROR_BADRESOURCE;
216 if (!SetResetCallback(callback))
217 return PP_ERROR_INPROGRESS;
219 FlushCommandBuffer();
221 return PP_OK_COMPLETIONPENDING;
224 void PPB_VideoDecoder_Impl::Destroy() {
225 FlushCommandBuffer();
228 decoder_.release()->Destroy();
229 ppp_videodecoder_ = NULL;
231 ::ppapi::PPB_VideoDecoder_Shared::Destroy();
234 void PPB_VideoDecoder_Impl::ProvidePictureBuffers(
235 uint32 requested_num_of_buffers,
236 const gfx::Size& dimensions,
237 uint32 texture_target) {
238 DCHECK(RenderThreadImpl::current());
239 if (!ppp_videodecoder_)
242 PP_Size out_dim = PP_MakeSize(dimensions.width(), dimensions.height());
243 ppp_videodecoder_->ProvidePictureBuffers(pp_instance(),
245 requested_num_of_buffers,
250 void PPB_VideoDecoder_Impl::PictureReady(const media::Picture& picture) {
251 DCHECK(RenderThreadImpl::current());
252 if (!ppp_videodecoder_)
255 PP_Picture_Dev output;
256 output.picture_buffer_id = picture.picture_buffer_id();
257 output.bitstream_buffer_id = picture.bitstream_buffer_id();
258 ppp_videodecoder_->PictureReady(pp_instance(), pp_resource(), &output);
261 void PPB_VideoDecoder_Impl::DismissPictureBuffer(int32 picture_buffer_id) {
262 DCHECK(RenderThreadImpl::current());
263 if (!ppp_videodecoder_)
265 ppp_videodecoder_->DismissPictureBuffer(
266 pp_instance(), pp_resource(), picture_buffer_id);
269 void PPB_VideoDecoder_Impl::NotifyError(
270 media::VideoDecodeAccelerator::Error error) {
271 DCHECK(RenderThreadImpl::current());
272 if (!ppp_videodecoder_)
275 PP_VideoDecodeError_Dev pp_error = MediaToPPError(error);
276 ppp_videodecoder_->NotifyError(pp_instance(), pp_resource(), pp_error);
277 UMA_HISTOGRAM_ENUMERATION("Media.PepperVideoDecoderError",
279 media::VideoDecodeAccelerator::LARGEST_ERROR_ENUM);
282 void PPB_VideoDecoder_Impl::NotifyResetDone() {
283 DCHECK(RenderThreadImpl::current());
284 RunResetCallback(PP_OK);
287 void PPB_VideoDecoder_Impl::NotifyEndOfBitstreamBuffer(
288 int32 bitstream_buffer_id) {
289 DCHECK(RenderThreadImpl::current());
290 RunBitstreamBufferCallback(bitstream_buffer_id, PP_OK);
293 void PPB_VideoDecoder_Impl::NotifyFlushDone() {
294 DCHECK(RenderThreadImpl::current());
295 RunFlushCallback(PP_OK);
298 } // namespace content