omxvideoenc: drain encoder on ALLOCATION and DRAIN queries
[platform/upstream/gstreamer.git] / omx / gstomxh264dec.c
1 /*
2  * Copyright (C) 2011, Hewlett-Packard Development Company, L.P.
3  *   Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>, Collabora Ltd.
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 "gstomxh264dec.h"
28 #include "gstomxh264utils.h"
29
30 GST_DEBUG_CATEGORY_STATIC (gst_omx_h264_dec_debug_category);
31 #define GST_CAT_DEFAULT gst_omx_h264_dec_debug_category
32
33 /* prototypes */
34 static gboolean gst_omx_h264_dec_is_format_change (GstOMXVideoDec * dec,
35     GstOMXPort * port, GstVideoCodecState * state);
36 static gboolean gst_omx_h264_dec_set_format (GstOMXVideoDec * dec,
37     GstOMXPort * port, GstVideoCodecState * state);
38
39 enum
40 {
41   PROP_0
42 };
43
44 /* class initialization */
45
46 #define DEBUG_INIT \
47   GST_DEBUG_CATEGORY_INIT (gst_omx_h264_dec_debug_category, "omxh264dec", 0, \
48       "debug category for gst-omx video decoder base class");
49
50 G_DEFINE_TYPE_WITH_CODE (GstOMXH264Dec, gst_omx_h264_dec,
51     GST_TYPE_OMX_VIDEO_DEC, DEBUG_INIT);
52
53 static void
54 gst_omx_h264_dec_class_init (GstOMXH264DecClass * klass)
55 {
56   GstOMXVideoDecClass *videodec_class = GST_OMX_VIDEO_DEC_CLASS (klass);
57   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
58
59   videodec_class->is_format_change =
60       GST_DEBUG_FUNCPTR (gst_omx_h264_dec_is_format_change);
61   videodec_class->set_format = GST_DEBUG_FUNCPTR (gst_omx_h264_dec_set_format);
62
63   videodec_class->cdata.default_sink_template_caps = "video/x-h264, "
64       "alignment=(string) au, "
65       "stream-format=(string) byte-stream, "
66       "width=(int) [1,MAX], " "height=(int) [1,MAX]";
67
68   gst_element_class_set_static_metadata (element_class,
69       "OpenMAX H.264 Video Decoder",
70       "Codec/Decoder/Video/Hardware",
71       "Decode H.264 video streams",
72       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
73
74   gst_omx_set_default_role (&videodec_class->cdata, "video_decoder.avc");
75 }
76
77 static void
78 gst_omx_h264_dec_init (GstOMXH264Dec * self)
79 {
80 }
81
82 static gboolean
83 gst_omx_h264_dec_is_format_change (GstOMXVideoDec * dec,
84     GstOMXPort * port, GstVideoCodecState * state)
85 {
86   GstCaps *old_caps = NULL;
87   GstCaps *new_caps = state->caps;
88   GstStructure *old_structure, *new_structure;
89   const gchar *old_profile, *old_level, *new_profile, *new_level;
90
91   if (dec->input_state) {
92     old_caps = dec->input_state->caps;
93   }
94
95   if (!old_caps) {
96     return FALSE;
97   }
98
99   old_structure = gst_caps_get_structure (old_caps, 0);
100   new_structure = gst_caps_get_structure (new_caps, 0);
101   old_profile = gst_structure_get_string (old_structure, "profile");
102   old_level = gst_structure_get_string (old_structure, "level");
103   new_profile = gst_structure_get_string (new_structure, "profile");
104   new_level = gst_structure_get_string (new_structure, "level");
105
106   if (g_strcmp0 (old_profile, new_profile) != 0
107       || g_strcmp0 (old_level, new_level) != 0) {
108     return TRUE;
109   }
110
111   return FALSE;
112 }
113
114 static gboolean
115 set_profile_and_level (GstOMXH264Dec * self, GstVideoCodecState * state)
116 {
117   OMX_ERRORTYPE err;
118   OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
119   const gchar *profile_string, *level_string;
120   GstStructure *s;
121
122   GST_OMX_INIT_STRUCT (&param);
123   param.nPortIndex = GST_OMX_VIDEO_DEC (self)->dec_in_port->index;
124
125   /* Pass profile and level to the decoder if we have both info from the
126    * caps. */
127   s = gst_caps_get_structure (state->caps, 0);
128   profile_string = gst_structure_get_string (s, "profile");
129   if (!profile_string)
130     return TRUE;
131
132   param.eProfile = gst_omx_h264_utils_get_profile_from_str (profile_string);
133   if (param.eProfile == OMX_VIDEO_AVCProfileMax)
134     goto unsupported_profile;
135
136   level_string = gst_structure_get_string (s, "level");
137   if (!level_string)
138     return TRUE;
139
140   param.eLevel = gst_omx_h264_utils_get_level_from_str (level_string);
141   if (param.eLevel == OMX_VIDEO_AVCLevelMax)
142     goto unsupported_level;
143
144   GST_DEBUG_OBJECT (self, "Set profile (%s) and level (%s) on decoder",
145       profile_string, level_string);
146
147   err =
148       gst_omx_component_set_parameter (GST_OMX_VIDEO_DEC (self)->dec,
149       OMX_IndexParamVideoProfileLevelCurrent, &param);
150   if (err == OMX_ErrorUnsupportedIndex) {
151     GST_WARNING_OBJECT (self,
152         "Setting profile/level not supported by component");
153   } else if (err != OMX_ErrorNone) {
154     GST_ERROR_OBJECT (self,
155         "Error setting profile %u and level %u: %s (0x%08x)",
156         (guint) param.eProfile, (guint) param.eLevel,
157         gst_omx_error_to_string (err), err);
158     return FALSE;
159   }
160
161   return TRUE;
162
163 unsupported_profile:
164   GST_ERROR_OBJECT (self, "Unsupported profile %s", profile_string);
165   return FALSE;
166
167 unsupported_level:
168   GST_ERROR_OBJECT (self, "Unsupported level %s", level_string);
169   return FALSE;
170 }
171
172 static gboolean
173 gst_omx_h264_dec_set_format (GstOMXVideoDec * dec, GstOMXPort * port,
174     GstVideoCodecState * state)
175 {
176   GstOMXVideoDecClass *klass = GST_OMX_VIDEO_DEC_GET_CLASS (dec);
177   OMX_PARAM_PORTDEFINITIONTYPE port_def;
178   OMX_ERRORTYPE err;
179
180   gst_omx_port_get_port_definition (port, &port_def);
181   port_def.format.video.eCompressionFormat = OMX_VIDEO_CodingAVC;
182   err = gst_omx_port_update_port_definition (port, &port_def);
183   if (err != OMX_ErrorNone)
184     return FALSE;
185
186   if (klass->cdata.hacks & GST_OMX_HACK_PASS_PROFILE_TO_DECODER) {
187     if (!set_profile_and_level (GST_OMX_H264_DEC (dec), state))
188       return FALSE;
189   }
190
191   return TRUE;
192 }