omxvideoenc: drain encoder on ALLOCATION and DRAIN queries
[platform/upstream/gstreamer.git] / omx / gstomxtheoradec.c
1 /*
2  * Copyright (C) 2013, Collabora Ltd.
3  *   Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation
8  * version 2.1 of the License.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <gst/gst.h>
26
27 #include "gstomxtheoradec.h"
28
29 GST_DEBUG_CATEGORY_STATIC (gst_omx_theora_dec_debug_category);
30 #define GST_CAT_DEFAULT gst_omx_theora_dec_debug_category
31
32 /* prototypes */
33 static gboolean gst_omx_theora_dec_is_format_change (GstOMXVideoDec * dec,
34     GstOMXPort * port, GstVideoCodecState * state);
35 static gboolean gst_omx_theora_dec_set_format (GstOMXVideoDec * dec,
36     GstOMXPort * port, GstVideoCodecState * state);
37 static GstFlowReturn gst_omx_theora_dec_handle_frame (GstVideoDecoder * decoder,
38     GstVideoCodecFrame * frame);
39 static gboolean gst_omx_theora_dec_stop (GstVideoDecoder * decoder);
40
41 enum
42 {
43   PROP_0
44 };
45
46 /* class initialization */
47
48 #define DEBUG_INIT \
49   GST_DEBUG_CATEGORY_INIT (gst_omx_theora_dec_debug_category, "omxtheoradec", 0, \
50       "debug category for gst-omx video decoder base class");
51
52 G_DEFINE_TYPE_WITH_CODE (GstOMXTheoraDec, gst_omx_theora_dec,
53     GST_TYPE_OMX_VIDEO_DEC, DEBUG_INIT);
54
55 static void
56 gst_omx_theora_dec_class_init (GstOMXTheoraDecClass * klass)
57 {
58   GstVideoDecoderClass *gstvideodec_class = GST_VIDEO_DECODER_CLASS (klass);
59   GstOMXVideoDecClass *videodec_class = GST_OMX_VIDEO_DEC_CLASS (klass);
60   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
61
62   videodec_class->is_format_change =
63       GST_DEBUG_FUNCPTR (gst_omx_theora_dec_is_format_change);
64   videodec_class->set_format =
65       GST_DEBUG_FUNCPTR (gst_omx_theora_dec_set_format);
66
67   videodec_class->cdata.default_sink_template_caps = "video/x-theora, "
68       "width=(int) [1,MAX], " "height=(int) [1,MAX]";
69
70   gstvideodec_class->handle_frame = gst_omx_theora_dec_handle_frame;
71   gstvideodec_class->stop = gst_omx_theora_dec_stop;
72
73   gst_element_class_set_static_metadata (element_class,
74       "OpenMAX Theora Video Decoder",
75       "Codec/Decoder/Video/Hardware",
76       "Decode Theora video streams",
77       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
78
79   gst_omx_set_default_role (&videodec_class->cdata, "video_decoder.theora");
80 }
81
82 static void
83 gst_omx_theora_dec_init (GstOMXTheoraDec * self)
84 {
85 }
86
87 static gboolean
88 gst_omx_theora_dec_is_format_change (GstOMXVideoDec * dec,
89     GstOMXPort * port, GstVideoCodecState * state)
90 {
91   return FALSE;
92 }
93
94 static gboolean
95 gst_omx_theora_dec_set_format (GstOMXVideoDec * dec, GstOMXPort * port,
96     GstVideoCodecState * state)
97 {
98   gboolean ret;
99   OMX_PARAM_PORTDEFINITIONTYPE port_def;
100
101   gst_omx_port_get_port_definition (port, &port_def);
102   port_def.format.video.eCompressionFormat = OMX_VIDEO_CodingTheora;
103   ret = gst_omx_port_update_port_definition (port, &port_def) == OMX_ErrorNone;
104
105   return ret;
106 }
107
108 static GstFlowReturn
109 gst_omx_theora_dec_handle_frame (GstVideoDecoder * decoder,
110     GstVideoCodecFrame * frame)
111 {
112   GstOMXTheoraDec *self = GST_OMX_THEORA_DEC (decoder);
113
114   if (GST_BUFFER_FLAG_IS_SET (frame->input_buffer, GST_BUFFER_FLAG_HEADER)) {
115     guint16 size;
116     GstBuffer *sbuf;
117
118     if (!self->header) {
119       self->header = gst_buffer_new ();
120       gst_buffer_copy_into (self->header, frame->input_buffer,
121           GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1);
122     }
123
124     size = gst_buffer_get_size (frame->input_buffer);
125     size = GUINT16_TO_BE (size);
126     sbuf = gst_buffer_new_and_alloc (2);
127     gst_buffer_fill (sbuf, 0, &size, 2);
128     self->header = gst_buffer_append (self->header, sbuf);
129
130     self->header =
131         gst_buffer_append (self->header, gst_buffer_ref (frame->input_buffer));
132
133     gst_video_decoder_drop_frame (GST_VIDEO_DECODER (self), frame);
134
135     return GST_FLOW_OK;
136   }
137
138   if (self->header) {
139     gst_buffer_replace (&GST_OMX_VIDEO_DEC (self)->codec_data, self->header);
140     gst_buffer_unref (self->header);
141     self->header = NULL;
142   }
143
144   return
145       GST_VIDEO_DECODER_CLASS (gst_omx_theora_dec_parent_class)->handle_frame
146       (GST_VIDEO_DECODER (self), frame);
147 }
148
149 static gboolean
150 gst_omx_theora_dec_stop (GstVideoDecoder * decoder)
151 {
152   GstOMXTheoraDec *self = GST_OMX_THEORA_DEC (decoder);
153
154   gst_buffer_replace (&self->header, NULL);
155
156   return TRUE;
157 }