omxvideoenc: drain encoder on ALLOCATION and DRAIN queries
[platform/upstream/gstreamer.git] / omx / gstomxvideo.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  * Copyright (C) 2013, Collabora Ltd.
5  *   Author: Sebastian Dröge <sebastian.droege@collabora.co.uk> *
6  * Copyright 2014 Advanced Micro Devices, Inc.
7  *   Author: Christian König <christian.koenig@amd.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation
12  * version 2.1 of the License.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
22  *
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include "gstomxvideo.h"
30
31 #include <math.h>
32
33 GST_DEBUG_CATEGORY (gst_omx_video_debug_category);
34 #define GST_CAT_DEFAULT gst_omx_video_debug_category
35
36 /* Keep synced with GST_OMX_VIDEO_SUPPORTED_FORMATS */
37 GstVideoFormat
38 gst_omx_video_get_format_from_omx (OMX_COLOR_FORMATTYPE omx_colorformat)
39 {
40   GstVideoFormat format;
41
42   switch (omx_colorformat) {
43     case OMX_COLOR_FormatL8:
44       format = GST_VIDEO_FORMAT_GRAY8;
45       break;
46     case OMX_COLOR_FormatYUV420Planar:
47     case OMX_COLOR_FormatYUV420PackedPlanar:
48       format = GST_VIDEO_FORMAT_I420;
49       break;
50     case OMX_COLOR_FormatYUV420SemiPlanar:
51     case OMX_COLOR_FormatYUV420PackedSemiPlanar:
52       format = GST_VIDEO_FORMAT_NV12;
53       break;
54     case OMX_COLOR_FormatYUV422SemiPlanar:
55       format = GST_VIDEO_FORMAT_NV16;
56       break;
57     case OMX_COLOR_FormatYCbYCr:
58       format = GST_VIDEO_FORMAT_YUY2;
59       break;
60     case OMX_COLOR_FormatYCrYCb:
61       format = GST_VIDEO_FORMAT_YVYU;
62       break;
63     case OMX_COLOR_FormatCbYCrY:
64       format = GST_VIDEO_FORMAT_UYVY;
65       break;
66     case OMX_COLOR_Format32bitARGB8888:
67       /* There is a mismatch in omxil specification 4.2.1 between
68        * OMX_COLOR_Format32bitARGB8888 and its description
69        * Follow the description */
70       format = GST_VIDEO_FORMAT_ABGR;
71       break;
72     case OMX_COLOR_Format32bitBGRA8888:
73       /* Same issue as OMX_COLOR_Format32bitARGB8888 */
74       format = GST_VIDEO_FORMAT_ARGB;
75       break;
76     case OMX_COLOR_Format16bitRGB565:
77       format = GST_VIDEO_FORMAT_RGB16;
78       break;
79     case OMX_COLOR_Format16bitBGR565:
80       format = GST_VIDEO_FORMAT_BGR16;
81       break;
82     case OMX_COLOR_Format24bitBGR888:
83       format = GST_VIDEO_FORMAT_BGR;
84       break;
85 #ifdef USE_OMX_TARGET_ZYNQ_USCALE_PLUS
86       /* Formats defined in extensions have their own enum so disable to -Wswitch warning */
87 #pragma GCC diagnostic push
88 #pragma GCC diagnostic ignored "-Wswitch"
89     case OMX_ALG_COLOR_FormatYUV420SemiPlanar10bitPacked:
90       format = GST_VIDEO_FORMAT_NV12_10LE32;
91       break;
92     case OMX_ALG_COLOR_FormatYUV422SemiPlanar10bitPacked:
93       format = GST_VIDEO_FORMAT_NV16_10LE32;
94       break;
95 #pragma GCC diagnostic pop
96 #endif
97     default:
98       format = GST_VIDEO_FORMAT_UNKNOWN;
99       break;
100   }
101
102   return format;
103 }
104
105 GList *
106 gst_omx_video_get_supported_colorformats (GstOMXPort * port,
107     GstVideoCodecState * state)
108 {
109   GstOMXComponent *comp = port->comp;
110   OMX_VIDEO_PARAM_PORTFORMATTYPE param;
111   OMX_ERRORTYPE err;
112   GList *negotiation_map = NULL;
113   gint old_index;
114   GstOMXVideoNegotiationMap *m;
115   GstVideoFormat f;
116
117   GST_OMX_INIT_STRUCT (&param);
118   param.nPortIndex = port->index;
119   param.nIndex = 0;
120   param.xFramerate =
121       state ? gst_omx_video_calculate_framerate_q16 (&state->info) : 0;
122
123   old_index = -1;
124   do {
125     err =
126         gst_omx_component_get_parameter (comp,
127         OMX_IndexParamVideoPortFormat, &param);
128
129     /* FIXME: Workaround for Bellagio that simply always
130      * returns the same value regardless of nIndex and
131      * never returns OMX_ErrorNoMore
132      */
133     if (old_index == param.nIndex)
134       break;
135
136     if (err == OMX_ErrorNone || err == OMX_ErrorNoMore) {
137       f = gst_omx_video_get_format_from_omx (param.eColorFormat);
138
139       if (f != GST_VIDEO_FORMAT_UNKNOWN) {
140         m = g_slice_new (GstOMXVideoNegotiationMap);
141         m->format = f;
142         m->type = param.eColorFormat;
143         negotiation_map = g_list_append (negotiation_map, m);
144         GST_DEBUG_OBJECT (comp->parent,
145             "Component port %d supports %s (%d) at index %u", port->index,
146             gst_video_format_to_string (f), param.eColorFormat,
147             (guint) param.nIndex);
148       } else {
149         GST_DEBUG_OBJECT (comp->parent,
150             "Component port %d supports unsupported color format %d at index %u",
151             port->index, param.eColorFormat, (guint) param.nIndex);
152       }
153     }
154     old_index = param.nIndex++;
155   } while (err == OMX_ErrorNone);
156
157   return negotiation_map;
158 }
159
160 GstCaps *
161 gst_omx_video_get_caps_for_map (GList * map)
162 {
163   GstCaps *caps = gst_caps_new_empty ();
164   GList *l;
165
166   for (l = map; l; l = l->next) {
167     GstOMXVideoNegotiationMap *entry = l->data;
168
169     gst_caps_append_structure (caps,
170         gst_structure_new ("video/x-raw",
171             "format", G_TYPE_STRING,
172             gst_video_format_to_string (entry->format), NULL));
173   }
174   return caps;
175 }
176
177 void
178 gst_omx_video_negotiation_map_free (GstOMXVideoNegotiationMap * m)
179 {
180   g_slice_free (GstOMXVideoNegotiationMap, m);
181 }
182
183 GstVideoCodecFrame *
184 gst_omx_video_find_nearest_frame (GstElement * element, GstOMXBuffer * buf,
185     GList * frames)
186 {
187   GstVideoCodecFrame *best = NULL;
188   GstClockTimeDiff best_diff = G_MAXINT64;
189   GstClockTime timestamp;
190   GList *l;
191
192   timestamp =
193       gst_util_uint64_scale (GST_OMX_GET_TICKS (buf->omx_buf->nTimeStamp),
194       GST_SECOND, OMX_TICKS_PER_SECOND);
195
196   GST_LOG_OBJECT (element, "look for ts %" GST_TIME_FORMAT,
197       GST_TIME_ARGS (timestamp));
198
199   for (l = frames; l; l = l->next) {
200     GstVideoCodecFrame *tmp = l->data;
201     GstClockTimeDiff diff = ABS (GST_CLOCK_DIFF (timestamp, tmp->pts));
202
203     GST_LOG_OBJECT (element,
204         "  frame %u diff %" G_GINT64_FORMAT " ts %" GST_TIME_FORMAT,
205         tmp->system_frame_number, diff, GST_TIME_ARGS (tmp->pts));
206
207     if (diff < best_diff) {
208       best = tmp;
209       best_diff = diff;
210
211       if (diff == 0)
212         break;
213     }
214   }
215
216   if (best) {
217     gst_video_codec_frame_ref (best);
218
219     /* OMX timestamps are in microseconds while gst ones are in nanoseconds.
220      * So if the difference between them is higher than 1 microsecond we likely
221      * picked the wrong frame. */
222     if (best_diff >= GST_USECOND)
223       GST_WARNING_OBJECT (element,
224           "Difference between ts (%" GST_TIME_FORMAT ") and frame %u (%"
225           GST_TIME_FORMAT ") seems too high (%" GST_TIME_FORMAT ")",
226           GST_TIME_ARGS (timestamp), best->system_frame_number,
227           GST_TIME_ARGS (best->pts), GST_TIME_ARGS (best_diff));
228   }
229
230   g_list_foreach (frames, (GFunc) gst_video_codec_frame_unref, NULL);
231   g_list_free (frames);
232
233   return best;
234 }
235
236 OMX_U32
237 gst_omx_video_calculate_framerate_q16 (GstVideoInfo * info)
238 {
239   g_assert (info);
240
241   return gst_util_uint64_scale_int (1 << 16, info->fps_n, info->fps_d);
242 }
243
244 gboolean
245 gst_omx_video_is_equal_framerate_q16 (OMX_U32 q16_a, OMX_U32 q16_b)
246 {
247   /* If one of them is 0 use the classic comparison. The value 0 has a special
248      meaning and is used to indicate the frame rate is unknown, variable, or
249      is not needed. */
250   if (!q16_a || !q16_b)
251     return q16_a == q16_b;
252
253   /* If the 'percentage change' is less than 1% then consider it equal to avoid
254    * an unnecessary re-negotiation. */
255   return fabs (((gdouble) q16_a) - ((gdouble) q16_b)) / (gdouble) q16_b < 0.01;
256 }