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/command_buffer_proxy_impl.h"
13 #include "content/renderer/pepper/host_globals.h"
14 #include "content/renderer/pepper/pepper_plugin_instance_impl.h"
15 #include "content/renderer/pepper/plugin_module.h"
16 #include "content/renderer/pepper/ppb_buffer_impl.h"
17 #include "content/renderer/pepper/ppb_graphics_3d_impl.h"
18 #include "content/renderer/render_thread_impl.h"
19 #include "gpu/command_buffer/client/gles2_implementation.h"
20 #include "media/video/picture.h"
21 #include "media/video/video_decode_accelerator.h"
22 #include "ppapi/c/dev/pp_video_dev.h"
23 #include "ppapi/c/dev/ppb_video_decoder_dev.h"
24 #include "ppapi/c/dev/ppp_video_decoder_dev.h"
25 #include "ppapi/c/pp_completion_callback.h"
26 #include "ppapi/c/pp_errors.h"
27 #include "ppapi/shared_impl/resource_tracker.h"
28 #include "ppapi/thunk/enter.h"
30 using ppapi::TrackedCallback
;
31 using ppapi::thunk::EnterResourceNoLock
;
32 using ppapi::thunk::PPB_Buffer_API
;
33 using ppapi::thunk::PPB_Graphics3D_API
;
37 // Convert PP_VideoDecoder_Profile to media::VideoCodecProfile.
38 media::VideoCodecProfile
PPToMediaProfile(
39 const PP_VideoDecoder_Profile pp_profile
) {
41 case PP_VIDEODECODER_H264PROFILE_NONE
:
42 // HACK: PPAPI contains a bogus "none" h264 profile that doesn't
43 // correspond to anything in h.264; but a number of released chromium
44 // versions silently promoted this to Baseline profile, so we retain that
45 // behavior here. Fall through.
46 case PP_VIDEODECODER_H264PROFILE_BASELINE
:
47 return media::H264PROFILE_BASELINE
;
48 case PP_VIDEODECODER_H264PROFILE_MAIN
:
49 return media::H264PROFILE_MAIN
;
50 case PP_VIDEODECODER_H264PROFILE_EXTENDED
:
51 return media::H264PROFILE_EXTENDED
;
52 case PP_VIDEODECODER_H264PROFILE_HIGH
:
53 return media::H264PROFILE_HIGH
;
54 case PP_VIDEODECODER_H264PROFILE_HIGH10PROFILE
:
55 return media::H264PROFILE_HIGH10PROFILE
;
56 case PP_VIDEODECODER_H264PROFILE_HIGH422PROFILE
:
57 return media::H264PROFILE_HIGH422PROFILE
;
58 case PP_VIDEODECODER_H264PROFILE_HIGH444PREDICTIVEPROFILE
:
59 return media::H264PROFILE_HIGH444PREDICTIVEPROFILE
;
60 case PP_VIDEODECODER_H264PROFILE_SCALABLEBASELINE
:
61 return media::H264PROFILE_SCALABLEBASELINE
;
62 case PP_VIDEODECODER_H264PROFILE_SCALABLEHIGH
:
63 return media::H264PROFILE_SCALABLEHIGH
;
64 case PP_VIDEODECODER_H264PROFILE_STEREOHIGH
:
65 return media::H264PROFILE_STEREOHIGH
;
66 case PP_VIDEODECODER_H264PROFILE_MULTIVIEWHIGH
:
67 return media::H264PROFILE_MULTIVIEWHIGH
;
68 case PP_VIDEODECODER_VP8PROFILE_ANY
:
69 return media::VP8PROFILE_ANY
;
71 return media::VIDEO_CODEC_PROFILE_UNKNOWN
;
75 PP_VideoDecodeError_Dev
MediaToPPError(
76 media::VideoDecodeAccelerator::Error error
) {
78 case media::VideoDecodeAccelerator::ILLEGAL_STATE
:
79 return PP_VIDEODECODERERROR_ILLEGAL_STATE
;
80 case media::VideoDecodeAccelerator::INVALID_ARGUMENT
:
81 return PP_VIDEODECODERERROR_INVALID_ARGUMENT
;
82 case media::VideoDecodeAccelerator::UNREADABLE_INPUT
:
83 return PP_VIDEODECODERERROR_UNREADABLE_INPUT
;
84 case media::VideoDecodeAccelerator::PLATFORM_FAILURE
:
85 return PP_VIDEODECODERERROR_PLATFORM_FAILURE
;
88 return PP_VIDEODECODERERROR_ILLEGAL_STATE
;
96 PPB_VideoDecoder_Impl::PPB_VideoDecoder_Impl(PP_Instance instance
)
97 : PPB_VideoDecoder_Shared(instance
), ppp_videodecoder_(NULL
) {
100 PPB_VideoDecoder_Impl::~PPB_VideoDecoder_Impl() { Destroy(); }
103 PP_Resource
PPB_VideoDecoder_Impl::Create(PP_Instance instance
,
104 PP_Resource graphics_context
,
105 PP_VideoDecoder_Profile profile
) {
106 scoped_refptr
<PPB_VideoDecoder_Impl
> decoder(
107 new PPB_VideoDecoder_Impl(instance
));
108 if (decoder
->Init(graphics_context
, profile
))
109 return decoder
->GetReference();
113 bool PPB_VideoDecoder_Impl::Init(PP_Resource graphics_context
,
114 PP_VideoDecoder_Profile profile
) {
115 EnterResourceNoLock
<PPB_Graphics3D_API
> enter_context(graphics_context
, true);
116 if (enter_context
.failed())
119 PPB_Graphics3D_Impl
* graphics_3d
=
120 static_cast<PPB_Graphics3D_Impl
*>(enter_context
.object());
122 CommandBufferProxyImpl
* command_buffer
= graphics_3d
->GetCommandBufferProxy();
126 InitCommon(graphics_context
, graphics_3d
->gles2_impl());
127 FlushCommandBuffer();
129 // This is not synchronous, but subsequent IPC messages will be buffered, so
130 // it is okay to immediately send IPC messages.
131 decoder_
= command_buffer
->CreateVideoDecoder();
132 return (decoder_
&& decoder_
->Initialize(PPToMediaProfile(profile
), this));
135 const PPP_VideoDecoder_Dev
* PPB_VideoDecoder_Impl::GetPPP() {
136 if (!ppp_videodecoder_
) {
137 PluginModule
* plugin_module
=
138 HostGlobals::Get()->GetInstance(pp_instance())->module();
140 ppp_videodecoder_
= static_cast<const PPP_VideoDecoder_Dev
*>(
141 plugin_module
->GetPluginInterface(PPP_VIDEODECODER_DEV_INTERFACE
));
144 return ppp_videodecoder_
;
147 int32_t PPB_VideoDecoder_Impl::Decode(
148 const PP_VideoBitstreamBuffer_Dev
* bitstream_buffer
,
149 scoped_refptr
<TrackedCallback
> callback
) {
151 return PP_ERROR_BADRESOURCE
;
153 EnterResourceNoLock
<PPB_Buffer_API
> enter(bitstream_buffer
->data
, true);
155 return PP_ERROR_FAILED
;
157 PPB_Buffer_Impl
* buffer
= static_cast<PPB_Buffer_Impl
*>(enter
.object());
158 DCHECK_GE(bitstream_buffer
->id
, 0);
159 media::BitstreamBuffer
decode_buffer(bitstream_buffer
->id
,
160 buffer
->shared_memory()->handle(),
161 bitstream_buffer
->size
);
162 if (!SetBitstreamBufferCallback(bitstream_buffer
->id
, callback
))
163 return PP_ERROR_BADARGUMENT
;
165 FlushCommandBuffer();
166 decoder_
->Decode(decode_buffer
);
167 return PP_OK_COMPLETIONPENDING
;
170 void PPB_VideoDecoder_Impl::AssignPictureBuffers(
171 uint32_t no_of_buffers
,
172 const PP_PictureBuffer_Dev
* buffers
) {
175 UMA_HISTOGRAM_COUNTS_100("Media.PepperVideoDecoderPictureCount",
178 std::vector
<media::PictureBuffer
> wrapped_buffers
;
179 for (uint32 i
= 0; i
< no_of_buffers
; i
++) {
180 PP_PictureBuffer_Dev in_buf
= buffers
[i
];
181 DCHECK_GE(in_buf
.id
, 0);
182 media::PictureBuffer
buffer(
184 gfx::Size(in_buf
.size
.width
, in_buf
.size
.height
),
186 wrapped_buffers
.push_back(buffer
);
187 UMA_HISTOGRAM_COUNTS_10000("Media.PepperVideoDecoderPictureHeight",
191 FlushCommandBuffer();
192 decoder_
->AssignPictureBuffers(wrapped_buffers
);
195 void PPB_VideoDecoder_Impl::ReusePictureBuffer(int32_t picture_buffer_id
) {
199 FlushCommandBuffer();
200 decoder_
->ReusePictureBuffer(picture_buffer_id
);
203 int32_t PPB_VideoDecoder_Impl::Flush(scoped_refptr
<TrackedCallback
> callback
) {
205 return PP_ERROR_BADRESOURCE
;
207 if (!SetFlushCallback(callback
))
208 return PP_ERROR_INPROGRESS
;
210 FlushCommandBuffer();
212 return PP_OK_COMPLETIONPENDING
;
215 int32_t PPB_VideoDecoder_Impl::Reset(scoped_refptr
<TrackedCallback
> callback
) {
217 return PP_ERROR_BADRESOURCE
;
219 if (!SetResetCallback(callback
))
220 return PP_ERROR_INPROGRESS
;
222 FlushCommandBuffer();
224 return PP_OK_COMPLETIONPENDING
;
227 void PPB_VideoDecoder_Impl::Destroy() {
228 FlushCommandBuffer();
231 ppp_videodecoder_
= NULL
;
233 ::ppapi::PPB_VideoDecoder_Shared::Destroy();
236 void PPB_VideoDecoder_Impl::ProvidePictureBuffers(
237 uint32 requested_num_of_buffers
,
238 const gfx::Size
& dimensions
,
239 uint32 texture_target
) {
240 DCHECK(RenderThreadImpl::current());
244 PP_Size out_dim
= PP_MakeSize(dimensions
.width(), dimensions
.height());
245 GetPPP()->ProvidePictureBuffers(pp_instance(), pp_resource(),
246 requested_num_of_buffers
, &out_dim
,
250 void PPB_VideoDecoder_Impl::PictureReady(const media::Picture
& picture
) {
251 // So far picture.visible_rect is not used. If used, visible_rect should
252 // be validated since it comes from GPU process and may not be trustworthy.
253 DCHECK(RenderThreadImpl::current());
257 PP_Picture_Dev output
;
258 output
.picture_buffer_id
= picture
.picture_buffer_id();
259 output
.bitstream_buffer_id
= picture
.bitstream_buffer_id();
260 GetPPP()->PictureReady(pp_instance(), pp_resource(), &output
);
263 void PPB_VideoDecoder_Impl::DismissPictureBuffer(int32 picture_buffer_id
) {
264 DCHECK(RenderThreadImpl::current());
267 GetPPP()->DismissPictureBuffer(pp_instance(), pp_resource(),
271 void PPB_VideoDecoder_Impl::NotifyError(
272 media::VideoDecodeAccelerator::Error error
) {
273 DCHECK(RenderThreadImpl::current());
277 PP_VideoDecodeError_Dev pp_error
= MediaToPPError(error
);
278 GetPPP()->NotifyError(pp_instance(), pp_resource(), pp_error
);
279 UMA_HISTOGRAM_ENUMERATION("Media.PepperVideoDecoderError",
281 media::VideoDecodeAccelerator::LARGEST_ERROR_ENUM
);
284 void PPB_VideoDecoder_Impl::NotifyResetDone() {
285 DCHECK(RenderThreadImpl::current());
286 RunResetCallback(PP_OK
);
289 void PPB_VideoDecoder_Impl::NotifyEndOfBitstreamBuffer(
290 int32 bitstream_buffer_id
) {
291 DCHECK(RenderThreadImpl::current());
292 RunBitstreamBufferCallback(bitstream_buffer_id
, PP_OK
);
295 void PPB_VideoDecoder_Impl::NotifyFlushDone() {
296 DCHECK(RenderThreadImpl::current());
297 RunFlushCallback(PP_OK
);
300 } // namespace content