gstreamer/omx/gstomxh264enc.c

351 lines
11 KiB
C
Raw Normal View History

2011-07-25 09:32:51 +00:00
/*
* Copyright (C) 2011, Hewlett-Packard Development Company, L.P.
* Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>, Collabora Ltd.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gst/gst.h>
#include "gstomxh264enc.h"
GST_DEBUG_CATEGORY_STATIC (gst_omx_h264_enc_debug_category);
#define GST_CAT_DEFAULT gst_omx_h264_enc_debug_category
/* prototypes */
static gboolean gst_omx_h264_enc_set_format (GstOMXVideoEnc * enc,
2012-04-12 19:57:32 +00:00
GstOMXPort * port, GstVideoInfo * info);
2011-07-25 09:32:51 +00:00
static GstCaps *gst_omx_h264_enc_get_caps (GstOMXVideoEnc * enc,
GstOMXPort * port, GstVideoState * state);
static GstFlowReturn gst_omx_h264_enc_handle_output_frame (GstOMXVideoEnc *
2012-04-12 19:57:32 +00:00
self, GstOMXPort * port, GstOMXBuffer * buf, GstVideoFrameState * frame);
2011-07-25 09:32:51 +00:00
enum
{
PROP_0
};
/* class initialization */
2012-04-12 19:57:32 +00:00
#define DEBUG_INIT \
2011-07-25 09:32:51 +00:00
GST_DEBUG_CATEGORY_INIT (gst_omx_h264_enc_debug_category, "omxh264enc", 0, \
"debug category for gst-omx video encoder base class");
2012-04-12 19:57:32 +00:00
G_DEFINE_TYPE_WITH_CODE (GstOMXH264Enc, gst_omx_h264_enc,
GST_TYPE_OMX_VIDEO_ENC, DEBUG_INIT);
2011-07-25 09:32:51 +00:00
static void
gst_omx_h264_enc_class_init (GstOMXH264EncClass * klass)
{
2012-04-12 19:57:32 +00:00
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
2011-07-25 09:32:51 +00:00
GstOMXVideoEncClass *videoenc_class = GST_OMX_VIDEO_ENC_CLASS (klass);
videoenc_class->set_format = GST_DEBUG_FUNCPTR (gst_omx_h264_enc_set_format);
videoenc_class->get_caps = GST_DEBUG_FUNCPTR (gst_omx_h264_enc_get_caps);
2012-04-12 19:57:32 +00:00
videoenc_class->cdata.default_src_template_caps = "video/x-h264, "
2011-07-25 09:32:51 +00:00
"width=(int) [ 16, 4096 ], " "height=(int) [ 16, 4096 ]";
videoenc_class->handle_output_frame =
GST_DEBUG_FUNCPTR (gst_omx_h264_enc_handle_output_frame);
2011-07-25 09:32:51 +00:00
2012-04-12 19:57:32 +00:00
gst_element_class_set_details_simple (element_class,
"OpenMAX H.264 Video Encoder",
"Codec/Encoder/Video",
"Encode H.264 video streams",
"Sebastian Dröge <sebastian.droege@collabora.co.uk>");
gst_omx_set_default_role (&videoenc_class->cdata, "video_encoder.avc");
2011-07-25 09:32:51 +00:00
}
static void
2012-04-12 19:57:32 +00:00
gst_omx_h264_enc_init (GstOMXH264Enc * self)
2011-07-25 09:32:51 +00:00
{
}
static gboolean
gst_omx_h264_enc_set_format (GstOMXVideoEnc * enc, GstOMXPort * port,
2012-04-12 19:57:32 +00:00
GstVideoInfo * info)
2011-07-25 09:32:51 +00:00
{
GstOMXH264Enc *self = GST_OMX_H264_ENC (enc);
GstCaps *peercaps;
OMX_VIDEO_AVCPROFILETYPE profile = OMX_VIDEO_AVCProfileBaseline;
OMX_VIDEO_AVCLEVELTYPE level = OMX_VIDEO_AVCLevel11;
OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
OMX_ERRORTYPE err;
2012-04-12 19:57:32 +00:00
const gchar *profile_string, *level_string;
2012-04-12 19:57:32 +00:00
peercaps = gst_pad_peer_query_caps (GST_BASE_VIDEO_CODEC_SRC_PAD (enc),
gst_pad_get_pad_template_caps (GST_BASE_VIDEO_CODEC_SRC_PAD (enc)));
if (peercaps) {
GstStructure *s;
2012-04-12 19:57:32 +00:00
if (gst_caps_is_empty (peercaps)) {
gst_caps_unref (peercaps);
GST_ERROR_OBJECT (self, "Empty caps");
return FALSE;
}
2012-04-12 19:57:32 +00:00
s = gst_caps_get_structure (peercaps, 0);
profile_string = gst_structure_get_string (s, "profile");
if (profile_string) {
if (g_str_equal (profile_string, "baseline")) {
profile = OMX_VIDEO_AVCProfileBaseline;
} else if (g_str_equal (profile_string, "main")) {
profile = OMX_VIDEO_AVCProfileMain;
} else if (g_str_equal (profile_string, "extended")) {
profile = OMX_VIDEO_AVCProfileExtended;
} else if (g_str_equal (profile_string, "high")) {
profile = OMX_VIDEO_AVCProfileHigh;
} else if (g_str_equal (profile_string, "high-10")) {
profile = OMX_VIDEO_AVCProfileHigh10;
} else if (g_str_equal (profile_string, "high-4:2:2")) {
profile = OMX_VIDEO_AVCProfileHigh422;
} else if (g_str_equal (profile_string, "high-4:4:4")) {
profile = OMX_VIDEO_AVCProfileHigh444;
} else {
2012-04-12 19:57:32 +00:00
goto unsupported_profile;
}
}
level_string = gst_structure_get_string (s, "level");
if (level_string) {
if (g_str_equal (level_string, "1")) {
level = OMX_VIDEO_AVCLevel1;
} else if (g_str_equal (level_string, "1b")) {
level = OMX_VIDEO_AVCLevel1b;
} else if (g_str_equal (level_string, "1.1")) {
level = OMX_VIDEO_AVCLevel11;
} else if (g_str_equal (level_string, "1.2")) {
level = OMX_VIDEO_AVCLevel12;
} else if (g_str_equal (level_string, "1.3")) {
level = OMX_VIDEO_AVCLevel13;
} else if (g_str_equal (level_string, "2")) {
level = OMX_VIDEO_AVCLevel2;
} else if (g_str_equal (level_string, "2.1")) {
level = OMX_VIDEO_AVCLevel21;
} else if (g_str_equal (level_string, "2.2")) {
level = OMX_VIDEO_AVCLevel22;
} else if (g_str_equal (level_string, "3")) {
level = OMX_VIDEO_AVCLevel3;
} else if (g_str_equal (level_string, "3.1")) {
level = OMX_VIDEO_AVCLevel31;
} else if (g_str_equal (level_string, "3.2")) {
level = OMX_VIDEO_AVCLevel32;
} else if (g_str_equal (level_string, "4")) {
level = OMX_VIDEO_AVCLevel4;
} else if (g_str_equal (level_string, "4.1")) {
level = OMX_VIDEO_AVCLevel41;
} else if (g_str_equal (level_string, "4.2")) {
level = OMX_VIDEO_AVCLevel42;
} else if (g_str_equal (level_string, "5")) {
level = OMX_VIDEO_AVCLevel5;
} else if (g_str_equal (level_string, "5.1")) {
level = OMX_VIDEO_AVCLevel51;
} else {
2012-04-12 19:57:32 +00:00
goto unsupported_level;
}
}
2012-04-12 19:57:32 +00:00
gst_caps_unref (peercaps);
}
GST_OMX_INIT_STRUCT (&param);
param.nPortIndex = GST_OMX_VIDEO_ENC (self)->out_port->index;
param.eProfile = profile;
param.eLevel = level;
err =
gst_omx_component_set_parameter (GST_OMX_VIDEO_ENC (self)->component,
OMX_IndexParamVideoProfileLevelCurrent, &param);
if (err == OMX_ErrorUnsupportedIndex) {
GST_WARNING_OBJECT (self,
"Setting profile/level not supported by component");
} else if (err != OMX_ErrorNone) {
GST_ERROR_OBJECT (self,
"Error setting profile %d and level %d: %s (0x%08x)", profile, level,
gst_omx_error_to_string (err), err);
return FALSE;
}
2011-07-25 09:32:51 +00:00
return TRUE;
2012-04-12 19:57:32 +00:00
unsupported_profile:
gst_caps_unref (peercaps);
GST_ERROR_OBJECT (self, "Unsupported profile %s", profile_string);
return FALSE;
unsupported_level:
gst_caps_unref (peercaps);
GST_ERROR_OBJECT (self, "Unsupported level %s", level_string);
return FALSE;
2011-07-25 09:32:51 +00:00
}
static GstCaps *
gst_omx_h264_enc_get_caps (GstOMXVideoEnc * enc, GstOMXPort * port,
GstVideoState * state)
{
GstOMXH264Enc *self = GST_OMX_H264_ENC (enc);
2011-07-25 09:32:51 +00:00
GstCaps *caps;
OMX_ERRORTYPE err;
OMX_VIDEO_PARAM_PROFILELEVELTYPE param;
const gchar *profile, *level;
caps =
gst_caps_new_simple ("video/x-h264", "width", G_TYPE_INT, state->width,
"height", G_TYPE_INT, state->height, NULL);
if (state->fps_n != 0)
gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION, state->fps_n,
state->fps_d, NULL);
if (state->par_n != 1 || state->par_d != 1)
gst_caps_set_simple (caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
state->par_n, state->par_d, NULL);
GST_OMX_INIT_STRUCT (&param);
param.nPortIndex = GST_OMX_VIDEO_ENC (self)->out_port->index;
err =
gst_omx_component_get_parameter (GST_OMX_VIDEO_ENC (self)->component,
OMX_IndexParamVideoProfileLevelCurrent, &param);
if (err != OMX_ErrorNone && err != OMX_ErrorUnsupportedIndex)
return NULL;
if (err == OMX_ErrorNone) {
switch (param.eProfile) {
case OMX_VIDEO_AVCProfileBaseline:
profile = "baseline";
break;
case OMX_VIDEO_AVCProfileMain:
profile = "main";
break;
case OMX_VIDEO_AVCProfileExtended:
profile = "extended";
break;
case OMX_VIDEO_AVCProfileHigh:
profile = "high";
break;
case OMX_VIDEO_AVCProfileHigh10:
profile = "high-10";
break;
case OMX_VIDEO_AVCProfileHigh422:
profile = "high-4:2:2";
break;
case OMX_VIDEO_AVCProfileHigh444:
profile = "high-4:4:4";
break;
default:
g_assert_not_reached ();
break;
}
switch (param.eLevel) {
case OMX_VIDEO_AVCLevel1:
level = "1";
break;
case OMX_VIDEO_AVCLevel1b:
level = "1b";
break;
case OMX_VIDEO_AVCLevel11:
level = "1.1";
break;
case OMX_VIDEO_AVCLevel12:
level = "1.2";
break;
case OMX_VIDEO_AVCLevel13:
level = "1.3";
break;
case OMX_VIDEO_AVCLevel2:
level = "2";
break;
case OMX_VIDEO_AVCLevel21:
level = "2.1";
break;
case OMX_VIDEO_AVCLevel22:
level = "2.2";
break;
case OMX_VIDEO_AVCLevel3:
level = "3";
break;
case OMX_VIDEO_AVCLevel31:
level = "3.1";
break;
case OMX_VIDEO_AVCLevel32:
level = "3.2";
break;
case OMX_VIDEO_AVCLevel4:
level = "4";
break;
case OMX_VIDEO_AVCLevel41:
level = "4.1";
break;
case OMX_VIDEO_AVCLevel42:
level = "4.2";
break;
case OMX_VIDEO_AVCLevel5:
level = "5";
break;
case OMX_VIDEO_AVCLevel51:
level = "5.1";
break;
default:
g_assert_not_reached ();
break;
}
gst_caps_set_simple (caps,
"profile", G_TYPE_STRING, profile, "level", G_TYPE_STRING, level, NULL);
}
2011-07-25 09:32:51 +00:00
return caps;
}
static GstFlowReturn
gst_omx_h264_enc_handle_output_frame (GstOMXVideoEnc * self, GstOMXPort * port,
2012-04-12 19:57:32 +00:00
GstOMXBuffer * buf, GstVideoFrameState * frame)
{
if (buf->omx_buf->nFlags & OMX_BUFFERFLAG_CODECCONFIG) {
/* The codec data is SPS/PPS with a startcode => bytestream stream format
* For bytestream stream format the SPS/PPS is only in-stream and not
* in the caps!
*/
if (buf->omx_buf->nFilledLen >= 4 &&
GST_READ_UINT32_BE (buf->omx_buf->pBuffer +
buf->omx_buf->nOffset) == 0x00000001) {
GstBuffer *hdrs;
2012-04-12 19:57:32 +00:00
GstMapInfo map = GST_MAP_INFO_INIT;
GST_DEBUG_OBJECT (self, "got codecconfig in byte-stream format");
buf->omx_buf->nFlags &= ~OMX_BUFFERFLAG_CODECCONFIG;
hdrs = gst_buffer_new_and_alloc (buf->omx_buf->nFilledLen);
2012-04-12 19:57:32 +00:00
gst_buffer_map (hdrs, &map, GST_MAP_WRITE);
memcpy (map.data,
buf->omx_buf->pBuffer + buf->omx_buf->nOffset,
buf->omx_buf->nFilledLen);
2012-04-12 19:57:32 +00:00
gst_buffer_unmap (hdrs, &map);
gst_base_video_encoder_set_headers (GST_BASE_VIDEO_ENCODER (self), hdrs);
gst_buffer_unref (hdrs);
}
}
2012-04-12 19:57:32 +00:00
return
GST_OMX_VIDEO_ENC_CLASS
(gst_omx_h264_enc_parent_class)->handle_output_frame (self, port, buf,
frame);
}