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/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 int command_buffer_route_id
= graphics_3d
->GetCommandBufferRouteId();
123 if (command_buffer_route_id
== 0)
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 through the returned channel.
131 GpuChannelHost
* channel
= graphics_3d
->channel();
133 decoder_
= channel
->CreateVideoDecoder(command_buffer_route_id
);
134 return (decoder_
&& decoder_
->Initialize(PPToMediaProfile(profile
), this));
137 const PPP_VideoDecoder_Dev
* PPB_VideoDecoder_Impl::GetPPP() {
138 if (!ppp_videodecoder_
) {
139 PluginModule
* plugin_module
=
140 HostGlobals::Get()->GetInstance(pp_instance())->module();
142 ppp_videodecoder_
= static_cast<const PPP_VideoDecoder_Dev
*>(
143 plugin_module
->GetPluginInterface(PPP_VIDEODECODER_DEV_INTERFACE
));
146 return ppp_videodecoder_
;
149 int32_t PPB_VideoDecoder_Impl::Decode(
150 const PP_VideoBitstreamBuffer_Dev
* bitstream_buffer
,
151 scoped_refptr
<TrackedCallback
> callback
) {
153 return PP_ERROR_BADRESOURCE
;
155 EnterResourceNoLock
<PPB_Buffer_API
> enter(bitstream_buffer
->data
, true);
157 return PP_ERROR_FAILED
;
159 PPB_Buffer_Impl
* buffer
= static_cast<PPB_Buffer_Impl
*>(enter
.object());
160 DCHECK_GE(bitstream_buffer
->id
, 0);
161 media::BitstreamBuffer
decode_buffer(bitstream_buffer
->id
,
162 buffer
->shared_memory()->handle(),
163 bitstream_buffer
->size
);
164 if (!SetBitstreamBufferCallback(bitstream_buffer
->id
, callback
))
165 return PP_ERROR_BADARGUMENT
;
167 FlushCommandBuffer();
168 decoder_
->Decode(decode_buffer
);
169 return PP_OK_COMPLETIONPENDING
;
172 void PPB_VideoDecoder_Impl::AssignPictureBuffers(
173 uint32_t no_of_buffers
,
174 const PP_PictureBuffer_Dev
* buffers
) {
177 UMA_HISTOGRAM_COUNTS_100("Media.PepperVideoDecoderPictureCount",
180 std::vector
<media::PictureBuffer
> wrapped_buffers
;
181 for (uint32 i
= 0; i
< no_of_buffers
; i
++) {
182 PP_PictureBuffer_Dev in_buf
= buffers
[i
];
183 DCHECK_GE(in_buf
.id
, 0);
184 media::PictureBuffer
buffer(
186 gfx::Size(in_buf
.size
.width
, in_buf
.size
.height
),
188 wrapped_buffers
.push_back(buffer
);
189 UMA_HISTOGRAM_COUNTS_10000("Media.PepperVideoDecoderPictureHeight",
193 FlushCommandBuffer();
194 decoder_
->AssignPictureBuffers(wrapped_buffers
);
197 void PPB_VideoDecoder_Impl::ReusePictureBuffer(int32_t picture_buffer_id
) {
201 FlushCommandBuffer();
202 decoder_
->ReusePictureBuffer(picture_buffer_id
);
205 int32_t PPB_VideoDecoder_Impl::Flush(scoped_refptr
<TrackedCallback
> callback
) {
207 return PP_ERROR_BADRESOURCE
;
209 if (!SetFlushCallback(callback
))
210 return PP_ERROR_INPROGRESS
;
212 FlushCommandBuffer();
214 return PP_OK_COMPLETIONPENDING
;
217 int32_t PPB_VideoDecoder_Impl::Reset(scoped_refptr
<TrackedCallback
> callback
) {
219 return PP_ERROR_BADRESOURCE
;
221 if (!SetResetCallback(callback
))
222 return PP_ERROR_INPROGRESS
;
224 FlushCommandBuffer();
226 return PP_OK_COMPLETIONPENDING
;
229 void PPB_VideoDecoder_Impl::Destroy() {
230 FlushCommandBuffer();
233 ppp_videodecoder_
= NULL
;
235 ::ppapi::PPB_VideoDecoder_Shared::Destroy();
238 void PPB_VideoDecoder_Impl::ProvidePictureBuffers(
239 uint32 requested_num_of_buffers
,
240 const gfx::Size
& dimensions
,
241 uint32 texture_target
) {
242 DCHECK(RenderThreadImpl::current());
246 PP_Size out_dim
= PP_MakeSize(dimensions
.width(), dimensions
.height());
247 GetPPP()->ProvidePictureBuffers(pp_instance(), pp_resource(),
248 requested_num_of_buffers
, &out_dim
,
252 void PPB_VideoDecoder_Impl::PictureReady(const media::Picture
& picture
) {
253 // So far picture.visible_rect is not used. If used, visible_rect should
254 // be validated since it comes from GPU process and may not be trustworthy.
255 DCHECK(RenderThreadImpl::current());
259 PP_Picture_Dev output
;
260 output
.picture_buffer_id
= picture
.picture_buffer_id();
261 output
.bitstream_buffer_id
= picture
.bitstream_buffer_id();
262 GetPPP()->PictureReady(pp_instance(), pp_resource(), &output
);
265 void PPB_VideoDecoder_Impl::DismissPictureBuffer(int32 picture_buffer_id
) {
266 DCHECK(RenderThreadImpl::current());
269 GetPPP()->DismissPictureBuffer(pp_instance(), pp_resource(),
273 void PPB_VideoDecoder_Impl::NotifyError(
274 media::VideoDecodeAccelerator::Error error
) {
275 DCHECK(RenderThreadImpl::current());
279 PP_VideoDecodeError_Dev pp_error
= MediaToPPError(error
);
280 GetPPP()->NotifyError(pp_instance(), pp_resource(), pp_error
);
281 UMA_HISTOGRAM_ENUMERATION("Media.PepperVideoDecoderError",
283 media::VideoDecodeAccelerator::LARGEST_ERROR_ENUM
);
286 void PPB_VideoDecoder_Impl::NotifyResetDone() {
287 DCHECK(RenderThreadImpl::current());
288 RunResetCallback(PP_OK
);
291 void PPB_VideoDecoder_Impl::NotifyEndOfBitstreamBuffer(
292 int32 bitstream_buffer_id
) {
293 DCHECK(RenderThreadImpl::current());
294 RunBitstreamBufferCallback(bitstream_buffer_id
, PP_OK
);
297 void PPB_VideoDecoder_Impl::NotifyFlushDone() {
298 DCHECK(RenderThreadImpl::current());
299 RunFlushCallback(PP_OK
);
302 } // namespace content