1 // Copyright (c) 2014 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 #ifndef PPAPI_CPP_VIDEO_DECODER_H_
6 #define PPAPI_CPP_VIDEO_DECODER_H_
8 #include "ppapi/c/pp_codecs.h"
9 #include "ppapi/c/pp_size.h"
10 #include "ppapi/cpp/completion_callback.h"
11 #include "ppapi/cpp/graphics_3d.h"
12 #include "ppapi/cpp/resource.h"
13 #include "ppapi/cpp/size.h"
16 /// This file defines the API to create and use a VideoDecoder resource.
22 /// Video decoder interface.
25 /// - Call Create() to create a new video decoder resource.
26 /// - Call Initialize() to initialize it with a 3d graphics context and the
27 /// desired codec profile.
28 /// - Call Decode() continuously (waiting for each previous call to complete) to
29 /// push bitstream buffers to the decoder.
30 /// - Call GetPicture() continuously (waiting for each previous call to
31 /// complete) to pull decoded pictures from the decoder.
32 /// - Call Flush() to signal end of stream to the decoder and perform shutdown
33 /// when it completes.
34 /// - Call Reset() to quickly stop the decoder (e.g. to implement Seek) and wait
35 /// for the callback before restarting decoding at another point.
36 /// - To destroy the decoder, the plugin should release all of its references to
37 /// it. Any pending callbacks will abort before the decoder is destroyed.
39 /// Available video codecs vary by platform.
40 /// All: theora, vorbis, vp8.
41 /// Chrome and ChromeOS: aac, h264.
43 class VideoDecoder
: public Resource
{
45 /// Default constructor for creating an is_null() <code>VideoDecoder</code>
49 /// A constructor used to create a <code>VideoDecoder</code> and associate it
50 /// with the provided <code>Instance</code>.
51 /// @param[in] instance The instance with which this resource will be
53 explicit VideoDecoder(const InstanceHandle
& instance
);
55 /// The copy constructor for <code>VideoDecoder</code>.
56 /// @param[in] other A reference to a <code>VideoDecoder</code>.
57 VideoDecoder(const VideoDecoder
& other
);
59 /// Initializes a video decoder resource. This should be called after Create()
60 /// and before any other functions.
62 /// @param[in] graphics3d_context A <code>PPB_Graphics3D</code> resource to
63 /// use during decoding.
64 /// @param[in] profile A <code>PP_VideoProfile</code> specifying the video
66 /// @param[in] acceleration A <code>PP_HardwareAcceleration</code> specifying
67 /// whether to use a hardware accelerated or a software implementation.
68 /// @param[in] callback A <code>PP_CompletionCallback</code> to be called upon
71 /// @return An int32_t containing an error code from <code>pp_errors.h</code>.
72 /// Returns PP_ERROR_NOTSUPPORTED if video decoding is not available, or the
73 /// requested profile is not supported. In this case, the client may call
74 /// Initialize() again with different parameters to find a good configuration.
75 int32_t Initialize(const Graphics3D
& graphics3d_context
,
76 PP_VideoProfile profile
,
77 PP_HardwareAcceleration acceleration
,
78 const CompletionCallback
& callback
);
80 /// Decodes a bitstream buffer. Copies |size| bytes of data from the plugin's
81 /// |buffer|. The plugin should wait until the decoder signals completion by
82 /// returning PP_OK or by running |callback| before calling Decode() again.
84 /// In general, each bitstream buffer should contain a demuxed bitstream frame
85 /// for the selected video codec. For example, H264 decoders expect to receive
86 /// one AnnexB NAL unit, including the 4 byte start code prefix, while VP8
87 /// decoders expect to receive a bitstream frame without the IVF frame header.
89 /// If the call to Decode() eventually results in a picture, the |decode_id|
90 /// parameter is copied into the returned picture. The plugin can use this to
91 /// associate decoded pictures with Decode() calls (e.g. to assign timestamps
92 /// or frame numbers to pictures.) This value is opaque to the API so the
93 /// plugin is free to pass any value.
95 /// @param[in] decode_id An optional value, chosen by the plugin, that can be
96 /// used to associate calls to Decode() with decoded pictures returned by
98 /// @param[in] size Buffer size in bytes.
99 /// @param[in] buffer Starting address of buffer.
100 /// @param[in] callback A <code>CompletionCallback</code> to be called on
103 /// @return An int32_t containing an error code from <code>pp_errors.h</code>.
104 /// Returns PP_ERROR_FAILED if the decoder isn't initialized or if a Flush()
105 /// or Reset() call is pending.
106 /// Returns PP_ERROR_INPROGRESS if there is another Decode() call pending.
107 /// Returns PP_ERROR_NOMEMORY if a bitstream buffer can't be created.
108 /// Returns PP_ERROR_ABORTED when Reset() is called while Decode() is pending.
109 int32_t Decode(uint32_t decode_id
,
112 const CompletionCallback
& callback
);
114 /// Gets the next picture from the decoder. The picture is valid after the
115 /// decoder signals completion by returning PP_OK or running |callback|. The
116 /// plugin can call GetPicture() again after the decoder signals completion.
117 /// When the plugin is finished using the picture, it should return it to the
118 /// system by calling RecyclePicture().
120 /// @param[in] callback A <code>CompletionCallbackWithOutput</code> to be
121 /// called on completion, and on success, to hold the picture descriptor.
123 /// @return An int32_t containing an error code from <code>pp_errors.h</code>.
124 /// Returns PP_ERROR_FAILED if the decoder isn't initialized or if a Reset()
126 /// Returns PP_ERROR_INPROGRESS if there is another GetPicture() call pending.
127 /// Returns PP_ERROR_ABORTED when Reset() is called, or if a call to Flush()
128 /// completes while GetPicture() is pending.
130 const CompletionCallbackWithOutput
<PP_VideoPicture
>& callback
);
132 /// Recycles a picture that the plugin has received from the decoder.
133 /// The plugin should call this as soon as it has finished using the texture
134 /// so the decoder can decode more pictures.
136 /// @param[in] picture A <code>PP_VideoPicture</code> to return to the
138 void RecyclePicture(const PP_VideoPicture
& picture
);
140 /// Flushes the decoder. The plugin should call Flush() when it reaches the
141 /// end of its video stream in order to stop cleanly. The decoder will run any
142 /// pending Decode() call to completion. The plugin should make no further
143 /// calls to the decoder other than GetPicture() and RecyclePicture() until
144 /// the decoder signals completion by running |callback|. Just before
145 /// completion, any pending GetPicture() call will complete by running its
146 /// callback with result PP_ERROR_ABORTED to signal that no more pictures are
147 /// available. Any pictures held by the plugin remain valid during and after
148 /// the flush and should be recycled back to the decoder.
150 /// @param[in] callback A <code>CompletionCallback</code> to be called on
153 /// @return An int32_t containing an error code from <code>pp_errors.h</code>.
154 /// Returns PP_ERROR_FAILED if the decoder isn't initialized.
155 int32_t Flush(const CompletionCallback
& callback
);
157 /// Resets the decoder as quickly as possible. The plugin can call Reset() to
158 /// skip to another position in the video stream. After Reset() returns, any
159 /// pending calls to Decode() and GetPicture()) abort, causing their callbacks
160 /// to run with PP_ERROR_ABORTED. The plugin should not make further calls to
161 /// the decoder other than RecyclePicture() until the decoder signals
162 /// completion by running |callback|. Any pictures held by the plugin remain
163 /// valid during and after the reset and should be recycled back to the
166 /// @param[in] callback A <code>CompletionCallback</code> to be called on
169 /// @return An int32_t containing an error code from <code>pp_errors.h</code>.
170 /// Returns PP_ERROR_FAILED if the decoder isn't initialized.
171 int32_t Reset(const CompletionCallback
& callback
);
176 #endif // PPAPI_CPP_VIDEO_DECODER_H_