2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) 2013 Samsung Electronics Co., Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
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 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 #include "gstmaruutils.h"
22 #include <gst/audio/multichannel.h>
23 #include <gst/pbutils/codec-utils.h>
26 gst_maru_smpfmt_depth (int smp_fmt)
50 GST_ERROR ("Unhandled sample format !");
61 GstAudioChannelPosition gst;
62 } _ff_to_gst_layout[] = {
64 CH_FRONT_LEFT, GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT}, {
65 CH_FRONT_RIGHT, GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT}, {
66 CH_FRONT_CENTER, GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER}, {
67 CH_LOW_FREQUENCY, GST_AUDIO_CHANNEL_POSITION_LFE}, {
68 CH_BACK_LEFT, GST_AUDIO_CHANNEL_POSITION_REAR_LEFT}, {
69 CH_BACK_RIGHT, GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT}, {
70 CH_FRONT_LEFT_OF_CENTER, GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER}, {
71 CH_FRONT_RIGHT_OF_CENTER, GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER}, {
72 CH_BACK_CENTER, GST_AUDIO_CHANNEL_POSITION_REAR_CENTER}, {
73 CH_SIDE_LEFT, GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT}, {
74 CH_SIDE_RIGHT, GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT}, {
75 CH_TOP_CENTER, GST_AUDIO_CHANNEL_POSITION_NONE}, {
76 CH_TOP_FRONT_LEFT, GST_AUDIO_CHANNEL_POSITION_NONE}, {
77 CH_TOP_FRONT_CENTER, GST_AUDIO_CHANNEL_POSITION_NONE}, {
78 CH_TOP_FRONT_RIGHT, GST_AUDIO_CHANNEL_POSITION_NONE}, {
79 CH_TOP_BACK_LEFT, GST_AUDIO_CHANNEL_POSITION_NONE}, {
80 CH_TOP_BACK_CENTER, GST_AUDIO_CHANNEL_POSITION_NONE}, {
81 CH_TOP_BACK_RIGHT, GST_AUDIO_CHANNEL_POSITION_NONE}, {
82 CH_STEREO_LEFT, GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT}, {
83 CH_STEREO_RIGHT, GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT}
86 static GstAudioChannelPosition *
87 gst_ff_channel_layout_to_gst (guint64 channel_layout, guint channels)
89 guint nchannels = 0, i, j;
90 GstAudioChannelPosition *pos = NULL;
91 gboolean none_layout = FALSE;
93 for (i = 0; i < 64; i++) {
94 if ((channel_layout & (G_GUINT64_CONSTANT (1) << i)) != 0) {
99 if (channel_layout == 0) {
100 nchannels = channels;
104 if (nchannels != channels) {
105 GST_ERROR ("Number of channels is different (%u != %u)", channels,
110 pos = g_new (GstAudioChannelPosition, nchannels);
112 for (i = 0, j = 0; i < G_N_ELEMENTS (_ff_to_gst_layout); i++) {
113 if ((channel_layout & _ff_to_gst_layout[i].ff) != 0) {
114 pos[j++] = _ff_to_gst_layout[i].gst;
116 if (_ff_to_gst_layout[i].gst == GST_AUDIO_CHANNEL_POSITION_NONE) {
122 if (j != nchannels) {
123 GST_WARNING ("Unknown channels in channel layout - assuming NONE layout");
127 if (!none_layout && !gst_audio_check_channel_positions (pos, nchannels)) {
128 GST_ERROR ("Invalid channel layout %" G_GUINT64_FORMAT
129 " - assuming NONE layout", channel_layout);
134 if (nchannels == 1) {
135 pos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_MONO;
136 } else if (nchannels == 2) {
137 pos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT;
138 pos[1] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT;
139 } else if (channel_layout == 0) {
143 for (i = 0; i < nchannels; i++) {
144 pos[i] = GST_AUDIO_CHANNEL_POSITION_NONE;
149 if (nchannels == 1 && pos[0] == GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER) {
150 GST_DEBUG ("mono common case; won't set channel positions");
153 } else if (nchannels == 2 && pos[0] == GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT
154 && pos[1] == GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT) {
155 GST_DEBUG ("stereo common case; won't set channel positions");
164 gst_maru_codectype_to_video_caps (CodecContext *ctx, const char *name,
165 gboolean encode, CodecElement *codec)
169 GST_DEBUG ("context: %p, codec: %s, encode: %d, pixel format: %d",
170 ctx, name, encode, ctx->video.pix_fmt);
173 caps = gst_maru_pixfmt_to_caps (ctx->video.pix_fmt, ctx, name);
179 caps = gst_caps_new_empty ();
180 for (i = 0; i <= PIX_FMT_NB; i++) {
181 temp = gst_maru_pixfmt_to_caps (i, encode ? &ctx : NULL, name);
183 gst_caps_append (caps, temp);
192 gst_maru_codectype_to_audio_caps (CodecContext *ctx, const char *name,
193 gboolean encode, CodecElement *codec)
195 GstCaps *caps = NULL;
197 GST_DEBUG ("context: %p, codec: %s, encode: %d, codec: %p",
198 ctx, name, encode, codec);
201 caps = gst_maru_smpfmt_to_caps (ctx->audio.sample_fmt, ctx, name);
202 } else if (codec && codec->sample_fmts[0] != -1){
206 caps = gst_caps_new_empty ();
207 for (i = 0; codec->sample_fmts[i] != -1; i++) {
208 int8_t sample_fmt = -1;
210 sample_fmt = codec->sample_fmts[i];
211 if (!strcmp(name, "aac") && encode) {
212 sample_fmt = SAMPLE_FMT_S16;
213 GST_DEBUG ("convert sample_fmt. codec %s, encode %d, sample_fmt %d",
214 name, encode, sample_fmt);
218 gst_maru_smpfmt_to_caps (sample_fmt, ctx, name);
220 gst_caps_append (caps, temp);
226 CodecContext ctx = {{0}, {0}, 0};
228 ctx.audio.channels = -1;
229 caps = gst_caps_new_empty ();
230 for (i = 0; i <= SAMPLE_FMT_DBL; i++) {
231 temp = gst_maru_smpfmt_to_caps (i, encode ? &ctx : NULL, name);
233 gst_caps_append (caps, temp);
242 gst_maru_codectype_to_caps (int media_type, CodecContext *ctx,
243 const char *name, gboolean encode)
247 switch (media_type) {
248 case AVMEDIA_TYPE_VIDEO:
250 gst_maru_codectype_to_video_caps (ctx, name, encode, NULL);
252 case AVMEDIA_TYPE_AUDIO:
254 gst_maru_codectype_to_audio_caps (ctx, name, encode, NULL);
265 gst_maru_caps_to_pixfmt (const GstCaps *caps, CodecContext *ctx, gboolean raw)
269 const GValue *par = NULL;
271 GST_DEBUG ("converting caps %" GST_PTR_FORMAT, caps);
272 g_return_if_fail (gst_caps_get_size (caps) == 1);
273 str = gst_caps_get_structure (caps, 0);
275 gst_structure_get_int (str, "width", &ctx->video.width);
276 gst_structure_get_int (str, "height", &ctx->video.height);
277 gst_structure_get_int (str, "bpp", &ctx->video.bpp);
279 fps = gst_structure_get_value (str, "framerate");
280 if (fps != NULL && GST_VALUE_HOLDS_FRACTION (fps)) {
281 ctx->video.fps_d = gst_value_get_fraction_numerator (fps);
282 ctx->video.fps_n = gst_value_get_fraction_denominator (fps);
283 ctx->video.ticks_per_frame = 1;
285 GST_DEBUG ("setting framerate %d/%d = %lf",
286 ctx->video.fps_d, ctx->video.fps_n,
287 1. * ctx->video.fps_d / ctx->video.fps_n);
290 par = gst_structure_get_value (str, "pixel-aspect-ratio");
291 if (par && GST_VALUE_HOLDS_FRACTION (par)) {
292 ctx->video.par_n = gst_value_get_fraction_numerator (par);
293 ctx->video.par_d = gst_value_get_fraction_denominator (par);
300 g_return_if_fail (fps != NULL && GST_VALUE_HOLDS_FRACTION (fps));
302 if (strcmp (gst_structure_get_name (str), "video/x-raw-yuv") == 0) {
305 if (gst_structure_get_fourcc (str, "format", &fourcc)) {
307 case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
308 ctx->video.pix_fmt = PIX_FMT_YUYV422;
310 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
311 ctx->video.pix_fmt = PIX_FMT_YUV420P;
313 case GST_MAKE_FOURCC ('A', '4', '2', '0'):
314 ctx->video.pix_fmt = PIX_FMT_YUVA420P;
316 case GST_MAKE_FOURCC ('Y', '4', '1', 'B'):
317 ctx->video.pix_fmt = PIX_FMT_YUV411P;
319 case GST_MAKE_FOURCC ('Y', '4', '2', 'B'):
320 ctx->video.pix_fmt = PIX_FMT_YUV422P;
322 case GST_MAKE_FOURCC ('Y', 'U', 'V', '9'):
323 ctx->video.pix_fmt = PIX_FMT_YUV410P;
327 } else if (strcmp (gst_structure_get_name (str), "video/x-raw-rgb") == 0) {
328 gint bpp = 0, rmask = 0, endianness = 0;
330 if (gst_structure_get_int (str, "bpp", &bpp) &&
331 gst_structure_get_int (str, "endianness", &endianness)) {
332 if (gst_structure_get_int (str, "red_mask", &rmask)) {
335 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
336 if (rmask == 0x00ff0000) {
338 if (rmask == 0x00ff0000) {
340 ctx->video.pix_fmt = PIX_FMT_RGB32;
344 if (rmask == 0x0000FF) {
345 ctx->video.pix_fmt = PIX_FMT_BGR24;
347 ctx->video.pix_fmt = PIX_FMT_RGB24;
351 if (endianness == G_BYTE_ORDER) {
352 ctx->video.pix_fmt = PIX_FMT_RGB565;
356 if (endianness == G_BYTE_ORDER) {
357 ctx->video.pix_fmt = PIX_FMT_RGB555;
366 ctx->video.pix_fmt = PIX_FMT_PAL8;
370 } else if (strcmp (gst_structure_get_name (str), "video/x-raw-gray") == 0) {
373 if (gst_structure_get_int (str, "bpp", &bpp)) {
376 ctx->video.pix_fmt = PIX_FMT_GRAY8;
384 gst_maru_caps_to_smpfmt (const GstCaps *caps, CodecContext *ctx, gboolean raw)
387 gint depth = 0, width = 0, endianness = 0;
388 gboolean signedness = FALSE;
391 g_return_if_fail (gst_caps_get_size (caps) == 1);
392 str = gst_caps_get_structure (caps, 0);
394 gst_structure_get_int (str, "channels", &ctx->audio.channels);
395 gst_structure_get_int (str, "rate", &ctx->audio.sample_rate);
396 gst_structure_get_int (str, "block_align", &ctx->audio.block_align);
397 gst_structure_get_int (str, "bitrate", &ctx->bit_rate);
403 name = gst_structure_get_name (str);
405 if (!strcmp (name, "audio/x-raw-float")) {
406 if (gst_structure_get_int (str, "width", &width) &&
407 gst_structure_get_int (str, "endianness", &endianness)) {
408 if (endianness == G_BYTE_ORDER) {
410 ctx->audio.sample_fmt = SAMPLE_FMT_FLT;
411 } else if (width == 64) {
412 ctx->audio.sample_fmt = SAMPLE_FMT_DBL;
417 if (gst_structure_get_int (str, "width", &width) &&
418 gst_structure_get_int (str, "depth", &depth) &&
419 gst_structure_get_boolean (str, "signed", &signedness) &&
420 gst_structure_get_int (str, "endianness", &endianness)) {
421 if ((endianness == G_BYTE_ORDER) && (signedness == TRUE)) {
422 if ((width == 16) && (depth == 16)) {
423 ctx->audio.sample_fmt = SAMPLE_FMT_S16;
424 } else if ((width == 32) && (depth == 32)) {
425 ctx->audio.sample_fmt = SAMPLE_FMT_S32;
433 gst_maru_caps_with_codecname (const char *name, int media_type,
434 const GstCaps *caps, CodecContext *ctx)
436 GstStructure *structure;
438 const GstBuffer *buf;
440 if (!ctx || !gst_caps_get_size (caps)) {
444 structure = gst_caps_get_structure (caps, 0);
446 if ((value = gst_structure_get_value (structure, "codec_data"))) {
450 buf = GST_BUFFER_CAST (gst_value_get_mini_object (value));
451 size = GST_BUFFER_SIZE (buf);
452 data = GST_BUFFER_DATA (buf);
453 GST_DEBUG ("extradata: %p, size: %d\n", data, size);
455 if (ctx->codecdata) {
456 g_free (ctx->codecdata);
460 g_malloc0 (GST_ROUND_UP_16 (size + FF_INPUT_BUFFER_PADDING_SIZE));
461 memcpy (ctx->codecdata, data, size);
462 ctx->codecdata_size = size;
464 if ((strcmp (name, "vc1") == 0) && size > 0 && data[0] == 0) {
465 ctx->codecdata[0] = (guint8) size;
467 } else if (ctx->codecdata == NULL) {
468 ctx->codecdata_size = 0;
469 ctx->codecdata = g_malloc0 (GST_ROUND_UP_16(FF_INPUT_BUFFER_PADDING_SIZE));
470 GST_DEBUG ("no extra data.\n");
473 if ((strcmp (name, "mpeg4") == 0)) {
474 const gchar *mime = gst_structure_get_name (structure);
476 if (!strcmp (mime, "video/x-divx")) {
477 ctx->codec_tag = GST_MAKE_FOURCC ('D', 'I', 'V', 'X');
478 } else if (!strcmp (mime, "video/x-xvid")) {
479 ctx->codec_tag = GST_MAKE_FOURCC ('X', 'V', 'I', 'D');
480 } else if (!strcmp (mime, "video/x-3ivx")) {
481 ctx->codec_tag = GST_MAKE_FOURCC ('3', 'I', 'V', '1');
482 } else if (!strcmp (mime, "video/mpeg")) {
483 ctx->codec_tag = GST_MAKE_FOURCC ('m', 'p', '4', 'v');
489 if (!gst_caps_is_fixed (caps)) {
493 switch (media_type) {
494 case AVMEDIA_TYPE_VIDEO:
495 gst_maru_caps_to_pixfmt (caps, ctx, FALSE);
498 case AVMEDIA_TYPE_AUDIO:
499 gst_maru_caps_to_smpfmt (caps, ctx, FALSE);
506 #define CODEC_NAME_BUFFER_SIZE 32
509 gst_maru_caps_to_codecname (const GstCaps *caps,
511 CodecContext *context)
513 const gchar *mimetype;
514 const GstStructure *str;
517 str = gst_caps_get_structure (caps, 0);
519 mimetype = gst_structure_get_name (str);
521 if (!strcmp (mimetype, "video/x-h263")) {
522 const gchar *h263version = gst_structure_get_string (str, "h263version");
523 if (h263version && !strcmp (h263version, "h263p")) {
524 g_strlcpy (codec_name, "h263p", CODEC_NAME_BUFFER_SIZE);
526 g_strlcpy (codec_name, "h263", CODEC_NAME_BUFFER_SIZE);
528 media_type = AVMEDIA_TYPE_VIDEO;
529 } else if (!strcmp (mimetype, "video/mpeg")) {
533 if (gst_structure_get_boolean (str, "systemstream", &sys_strm) &&
534 gst_structure_get_int (str, "mpegversion", &mpegversion) &&
536 switch (mpegversion) {
538 g_strlcpy (codec_name, "mpeg1video", CODEC_NAME_BUFFER_SIZE);
541 g_strlcpy (codec_name, "mpeg2video", CODEC_NAME_BUFFER_SIZE);
544 g_strlcpy (codec_name, "mpeg4", CODEC_NAME_BUFFER_SIZE);
549 media_type = AVMEDIA_TYPE_VIDEO;
550 } else if (!strcmp (mimetype, "video/x-wmv")) {
553 if (gst_structure_get_int (str, "wmvversion", &wmvversion)) {
554 switch (wmvversion) {
556 g_strlcpy (codec_name, "wmv1", CODEC_NAME_BUFFER_SIZE);
559 g_strlcpy (codec_name, "wmv2", CODEC_NAME_BUFFER_SIZE);
565 g_strlcpy (codec_name, "wmv3", CODEC_NAME_BUFFER_SIZE);
566 if (gst_structure_get_fourcc (str, "format", &fourcc)) {
567 if ((fourcc == GST_MAKE_FOURCC ('W', 'V', 'C', '1')) ||
568 (fourcc == GST_MAKE_FOURCC ('W', 'M', 'V', 'A'))) {
569 g_strlcpy (codec_name, "vc1", CODEC_NAME_BUFFER_SIZE);
577 media_type = AVMEDIA_TYPE_VIDEO;
578 } else if (!strcmp (mimetype, "audio/mpeg")) {
580 gint mpegversion = 0;
582 if (gst_structure_get_int (str, "mpegversion", &mpegversion)) {
583 switch (mpegversion) {
586 g_strlcpy (codec_name, "aac", CODEC_NAME_BUFFER_SIZE);
589 if (gst_structure_get_int (str, "layer", &layer)) {
592 g_strlcpy (codec_name, "mp1", CODEC_NAME_BUFFER_SIZE);
595 g_strlcpy (codec_name, "mp2", CODEC_NAME_BUFFER_SIZE);
598 g_strlcpy (codec_name, "mp3", CODEC_NAME_BUFFER_SIZE);
606 media_type = AVMEDIA_TYPE_AUDIO;
607 } else if (!strcmp (mimetype, "audio/x-wma")) {
610 if (gst_structure_get_int (str, "wmaversion", &wmaversion)) {
611 switch (wmaversion) {
613 g_strlcpy (codec_name, "wmav1", CODEC_NAME_BUFFER_SIZE);
616 g_strlcpy (codec_name, "wmav2", CODEC_NAME_BUFFER_SIZE);
619 g_strlcpy (codec_name, "wmapro", CODEC_NAME_BUFFER_SIZE);
624 media_type = AVMEDIA_TYPE_AUDIO;
625 } else if (!strcmp (mimetype, "audio/x-ac3")) {
626 g_strlcpy (codec_name, "ac3", CODEC_NAME_BUFFER_SIZE);
627 media_type = AVMEDIA_TYPE_AUDIO;
628 } else if (!strcmp (mimetype, "audio/x-msmpeg")) {
629 gint msmpegversion = 0;
631 if (gst_structure_get_int (str, "msmpegversion", &msmpegversion)) {
632 switch (msmpegversion) {
634 g_strlcpy (codec_name, "msmpeg4v1", CODEC_NAME_BUFFER_SIZE);
637 g_strlcpy (codec_name, "msmpeg4v2", CODEC_NAME_BUFFER_SIZE);
640 g_strlcpy (codec_name, "msmpeg4", CODEC_NAME_BUFFER_SIZE);
645 media_type = AVMEDIA_TYPE_VIDEO;
646 } else if (!strcmp (mimetype, "video/x-h264")) {
647 g_strlcpy (codec_name, "h264", CODEC_NAME_BUFFER_SIZE);
648 media_type = AVMEDIA_TYPE_VIDEO;
651 if (context != NULL) {
654 context->codec_type = CODEC_TYPE_VIDEO;
655 } else if (audio == TRUE) {
656 context->codec_type = CODEC_TYPE_AUDIO;
658 context->codec_type = CODEC_TYPE_UNKNOWN;
661 gst_maru_caps_with_codecname (codec_name, media_type, caps, context);
664 if (codec_name != NULL) {
665 GST_DEBUG ("The %s belongs to the caps %" GST_PTR_FORMAT, codec_name, caps);
667 GST_WARNING ("Couldn't figure out the name for caps %" GST_PTR_FORMAT, caps);
672 gst_maru_caps_with_codectype (int media_type, const GstCaps *caps, CodecContext *ctx)
678 switch (media_type) {
679 case AVMEDIA_TYPE_VIDEO:
680 gst_maru_caps_to_pixfmt (caps, ctx, TRUE);
682 case AVMEDIA_TYPE_AUDIO:
683 gst_maru_caps_to_smpfmt (caps, ctx, TRUE);
691 gst_maru_video_caps_new (CodecContext *ctx, const char *name,
692 const char *mimetype, const char *fieldname, ...)
694 GstStructure *structure = NULL;
695 GstCaps *caps = NULL;
699 GST_LOG ("context: %p, name: %s, mimetype: %s", ctx, name, mimetype);
701 if (ctx != NULL && ctx->video.width != -1) {
704 caps = gst_caps_new_simple (mimetype,
705 "width", G_TYPE_INT, ctx->video.width,
706 "height", G_TYPE_INT, ctx->video.height, NULL);
708 num = ctx->video.fps_d / ctx->video.ticks_per_frame;
709 denom = ctx->video.fps_n;
712 GST_LOG ("invalid framerate: %d/0, -> %d/1", num, num);
714 if (gst_util_fraction_compare (num, denom, 1000, 1) > 0) {
715 GST_LOG ("excessive framerate: %d/%d, -> 0/1", num, denom);
719 GST_LOG ("setting framerate: %d/%d", num, denom);
720 gst_caps_set_simple (caps,
721 "framerate", GST_TYPE_FRACTION, num, denom, NULL);
723 if (strcmp (name, "h263") == 0) {
724 /* 128x96, 176x144, 352x288, 704x576, and 1408x1152. slightly reordered
725 * because we want automatic negotiation to go as close to 320x240 as
727 const static gint widths[] = { 352, 704, 176, 1408, 128 };
728 const static gint heights[] = { 288, 576, 144, 1152, 96 };
730 gint n_sizes = G_N_ELEMENTS (widths);
732 caps = gst_caps_new_empty ();
733 for (i = 0; i < n_sizes; i++) {
734 temp = gst_caps_new_simple (mimetype,
735 "width", G_TYPE_INT, widths[i],
736 "height", G_TYPE_INT, heights[i],
737 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
739 gst_caps_append (caps, temp);
741 } else if (strcmp (name, "none") == 0) {
742 GST_LOG ("default caps");
746 /* no fixed caps or special restrictions applied;
747 * default unfixed setting */
749 GST_DEBUG ("Creating default caps");
750 caps = gst_caps_new_simple (mimetype,
751 "width", GST_TYPE_INT_RANGE, 16, 4096,
752 "height", GST_TYPE_INT_RANGE, 16, 4096,
753 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
756 for (i = 0; i < gst_caps_get_size (caps); i++) {
757 va_start (var_args, fieldname);
758 structure = gst_caps_get_structure (caps, i);
759 gst_structure_set_valist (structure, fieldname, var_args);
767 gst_maru_audio_caps_new (CodecContext *ctx, const char *name,
768 const char *mimetype, const char *fieldname, ...)
770 GstStructure *structure = NULL;
771 GstCaps *caps = NULL;
775 if (ctx != NULL && ctx->audio.channels != -1) {
776 GstAudioChannelPosition *pos;
777 guint64 channel_layout = ctx->audio.channel_layout;
779 if (channel_layout == 0) {
780 const guint64 default_channel_set[] = {
781 0, 0, CH_LAYOUT_SURROUND, CH_LAYOUT_QUAD, CH_LAYOUT_5POINT0,
782 CH_LAYOUT_5POINT1, 0, CH_LAYOUT_7POINT1
785 if (strcmp (name, "ac3") == 0) {
786 if (ctx->audio.channels > 0 &&
787 ctx->audio.channels < G_N_ELEMENTS (default_channel_set)) {
788 channel_layout = default_channel_set[ctx->audio.channels - 1];
795 caps = gst_caps_new_simple (mimetype,
796 "rate", G_TYPE_INT, ctx->audio.sample_rate,
797 "channels", G_TYPE_INT, ctx->audio.channels, NULL);
799 pos = gst_ff_channel_layout_to_gst (channel_layout, ctx->audio.channels);
801 gst_audio_set_channel_positions (gst_caps_get_structure (caps, 0), pos);
805 gint maxchannels = 2;
806 const gint *rates = NULL;
809 if (strcmp (name, "aac") == 0) {
811 } else if (g_str_has_prefix(name, "ac3")) {
812 const static gint l_rates[] = { 48000, 44100, 32000 };
814 n_rates = G_N_ELEMENTS (l_rates);
820 if (maxchannels == 1) {
821 caps = gst_caps_new_simple(mimetype,
822 "channels", G_TYPE_INT, maxchannels, NULL);
824 caps = gst_caps_new_simple(mimetype,
825 "channels", GST_TYPE_INT_RANGE, 1, maxchannels, NULL);
829 GValue list = { 0, };
830 GstStructure *structure;
832 g_value_init(&list, GST_TYPE_LIST);
833 for (i = 0; i < n_rates; i++) {
836 g_value_init(&v, G_TYPE_INT);
837 g_value_set_int(&v, rates[i]);
838 gst_value_list_append_value(&list, &v);
841 structure = gst_caps_get_structure(caps, 0);
842 gst_structure_set_value(structure, "rate", &list);
843 g_value_unset(&list);
845 gst_caps_set_simple(caps, "rate", GST_TYPE_INT_RANGE, 4000, 96000, NULL);
849 for (i = 0; i < gst_caps_get_size (caps); i++) {
850 va_start (var_args, fieldname);
851 structure = gst_caps_get_structure (caps, i);
852 gst_structure_set_valist (structure, fieldname, var_args);
860 gst_maru_pixfmt_to_caps (enum PixelFormat pix_fmt, CodecContext *ctx, const char *name)
862 GstCaps *caps = NULL;
864 int bpp = 0, depth = 0, endianness = 0;
865 gulong g_mask = 0, r_mask = 0, b_mask = 0, a_mask = 0;
869 case PIX_FMT_YUV420P:
870 fmt = GST_MAKE_FOURCC ('I', '4', '2', '0');
872 case PIX_FMT_YUYV422:
873 fmt = GST_MAKE_FOURCC ('A', '4', '2', '0');
877 endianness = G_BIG_ENDIAN;
884 endianness = G_BIG_ENDIAN;
889 case PIX_FMT_YUV422P:
890 fmt = GST_MAKE_FOURCC ('Y', '4', '2', 'B');
892 case PIX_FMT_YUV444P:
893 fmt = GST_MAKE_FOURCC ('Y', '4', '4', '4');
898 endianness = G_BIG_ENDIAN;
899 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
911 case PIX_FMT_YUV410P:
912 fmt = GST_MAKE_FOURCC ('Y', 'U', 'V', '9');
914 case PIX_FMT_YUV411P:
915 fmt = GST_MAKE_FOURCC ('Y', '4', '1', 'b');
919 endianness = G_BYTE_ORDER;
927 endianness = G_BYTE_ORDER;
940 caps = gst_maru_video_caps_new (ctx, name, "video/x-raw-rgb",
941 "bpp", G_TYPE_INT, bpp,
942 "depth", G_TYPE_INT, depth,
943 "red_mask", G_TYPE_INT, r_mask,
944 "green_mask", G_TYPE_INT, g_mask,
945 "blue_mask", G_TYPE_INT, b_mask,
946 "alpha_mask", G_TYPE_INT, a_mask,
947 "endianness", G_TYPE_INT, endianness, NULL);
949 caps = gst_maru_video_caps_new (ctx, name, "video/x-raw-rgb",
950 "bpp", G_TYPE_INT, bpp,
951 "depth", G_TYPE_INT, depth,
952 "red_mask", G_TYPE_INT, r_mask,
953 "green_mask", G_TYPE_INT, g_mask,
954 "blue_mask", G_TYPE_INT, b_mask,
955 "alpha_mask", G_TYPE_INT, a_mask,
956 "endianness", G_TYPE_INT, endianness, NULL);
959 caps = gst_maru_video_caps_new (ctx, name, "video/x-raw-rgb",
960 "bpp", G_TYPE_INT, bpp,
961 "depth", G_TYPE_INT, depth,
962 "endianness", G_TYPE_INT, endianness, NULL);
968 caps = gst_maru_video_caps_new (ctx, name, "video/x-raw-yuv",
969 "format", GST_TYPE_FOURCC, fmt, NULL);
974 GST_DEBUG ("caps for pix_fmt=%d: %", GST_PTR_FORMAT, pix_fmt, caps);
976 GST_LOG ("No caps found for pix_fmt=%d", pix_fmt);
983 gst_maru_smpfmt_to_caps (int8_t sample_fmt, CodecContext *ctx, const char *name)
985 GstCaps *caps = NULL;
988 gboolean integer = TRUE;
989 gboolean signedness = FALSE;
991 switch (sample_fmt) {
993 case SAMPLE_FMT_S16P:
998 case SAMPLE_FMT_S32P:
1002 case SAMPLE_FMT_FLT:
1003 case SAMPLE_FMT_FLTP:
1007 case SAMPLE_FMT_DBL:
1008 case SAMPLE_FMT_DBLP:
1016 GST_DEBUG ("sample format: %d", sample_fmt);
1020 caps = gst_maru_audio_caps_new (ctx, name, "audio/x-raw-int",
1021 "signed", G_TYPE_BOOLEAN, signedness,
1022 "endianness", G_TYPE_INT, G_BYTE_ORDER,
1023 "width", G_TYPE_INT, bpp, "depth", G_TYPE_INT, bpp, NULL);
1025 caps = gst_maru_audio_caps_new (ctx, name, "audio/x-raw-float",
1026 "endianness", G_TYPE_INT, G_BYTE_ORDER,
1027 "width", G_TYPE_INT, bpp, NULL);
1032 GST_LOG ("caps for sample_fmt=%d: %" GST_PTR_FORMAT, sample_fmt, caps);
1034 GST_LOG ("No caps found for sample_fmt=%d", sample_fmt);
1041 gst_maru_codecname_to_caps (const char *name, CodecContext *ctx, gboolean encode)
1043 GstCaps *caps = NULL;
1045 GST_LOG ("codec: %s, context: %p, encode: %d", name, ctx, encode);
1047 if (strcmp (name, "mpegvideo") == 0) {
1048 caps = gst_maru_video_caps_new (ctx, name, "video/mpeg",
1049 "mpegversion", G_TYPE_INT, 1,
1050 "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
1051 } else if (strcmp (name, "h263") == 0) {
1053 caps = gst_maru_video_caps_new (ctx, name, "video/x-h263",
1054 "variant", G_TYPE_STRING, "itu", NULL);
1056 caps = gst_maru_video_caps_new (ctx, "none", "video/x-h263",
1057 "variant", G_TYPE_STRING, "itu", NULL);
1059 } else if (strcmp (name, "h263p") == 0) {
1060 caps = gst_maru_video_caps_new (ctx, name, "video/x-h263",
1061 "variant", G_TYPE_STRING, "itu",
1062 "h263version", G_TYPE_STRING, "h263p", NULL);
1064 if (encode && ctx) {
1065 gst_caps_set_simple (caps,
1066 "annex-f", G_TYPE_BOOLEAN, ctx->flags & CODEC_FLAG_4MV,
1067 "annex-j", G_TYPE_BOOLEAN, ctx->flags & CODEC_FLAG_LOOP_FILTER,
1068 "annex-i", G_TYPE_BOOLEAN, ctx->flags & CODEC_FLAG_AC_PRED,
1069 "annex-t", G_TYPE_BOOLEAN, ctx->flags & CODEC_FLAG_AC_PRED,
1073 } else if (strcmp (name, "mpeg2video") == 0) {
1075 caps = gst_maru_video_caps_new (ctx, name, "video/mpeg",
1076 "mpegversion", G_TYPE_INT, 2,
1077 "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
1079 caps = gst_caps_new_simple ("video/mpeg",
1080 "mpegversion", GST_TYPE_INT_RANGE, 1, 2,
1081 "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
1083 } else if (strcmp (name, "mpeg4") == 0) {
1084 if (encode && ctx != NULL) {
1086 switch (ctx->codec_tag) {
1087 case GST_MAKE_FOURCC ('D', 'I', 'V', 'X'):
1088 caps = gst_maru_video_caps_new (ctx, name, "video/x-divx",
1089 "divxversion", G_TYPE_INT, 5, NULL);
1091 case GST_MAKE_FOURCC ('m', 'p', '4', 'v'):
1093 caps = gst_maru_video_caps_new (ctx, name, "video/mpeg",
1094 "systemstream", G_TYPE_BOOLEAN, FALSE,
1095 "mpegversion", G_TYPE_INT, 4, NULL);
1099 caps = gst_maru_video_caps_new (ctx, name, "video/mpeg",
1100 "mpegversion", G_TYPE_INT, 4,
1101 "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
1103 caps = gst_maru_video_caps_new (ctx, name, "video/mpeg",
1104 "mpegversion", G_TYPE_INT, 4,
1105 "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
1107 gst_caps_append (caps, gst_maru_video_caps_new (ctx, name,
1108 "video/x-divx", "divxversion", GST_TYPE_INT_RANGE, 4, 5, NULL));
1109 gst_caps_append (caps, gst_maru_video_caps_new (ctx, name,
1110 "video/x-xvid", NULL));
1111 gst_caps_append (caps, gst_maru_video_caps_new (ctx, name,
1112 "video/x-3ivx", NULL));
1115 } else if (strcmp (name, "h264") == 0) {
1116 caps = gst_maru_video_caps_new (ctx, name, "video/x-h264", NULL);
1117 } else if (g_str_has_prefix(name, "msmpeg4")) {
1118 // msmpeg4v1,m msmpeg4v2, msmpeg4
1121 if (strcmp (name, "msmpeg4v1") == 0) {
1123 } else if (strcmp (name, "msmpeg4v2") == 0) {
1129 caps = gst_maru_video_caps_new (ctx, name, "video/x-msmpeg",
1130 "msmpegversion", G_TYPE_INT, version, NULL);
1131 if (!encode && !strcmp (name, "msmpeg4")) {
1132 gst_caps_append (caps, gst_maru_video_caps_new (ctx, name,
1133 "video/x-divx", "divxversion", G_TYPE_INT, 3, NULL));
1135 } else if (strcmp (name, "wmv3") == 0) {
1136 caps = gst_maru_video_caps_new (ctx, name, "video/x-wmv",
1137 "wmvversion", G_TYPE_INT, 3, NULL);
1138 } else if (strcmp (name, "vc1") == 0) {
1139 caps = gst_maru_video_caps_new (ctx, name, "video/x-wmv",
1140 "wmvversion", G_TYPE_INT, 3, "format", GST_TYPE_FOURCC,
1141 GST_MAKE_FOURCC ('W', 'V', 'C', '1'), NULL);
1143 } else if (strcmp (name, "vp3") == 0) {
1144 mime_type = g_strdup ("video/x-vp3");
1145 } else if (strcmp (name, "vp8") == 0) {
1146 mime_type = g_strdup ("video/x-vp8");
1148 } else if (strcmp (name, "aac") == 0) {
1149 caps = gst_maru_audio_caps_new (ctx, name, "audio/mpeg", NULL);
1151 GValue arr = { 0, };
1152 GValue item = { 0, };
1154 g_value_init (&arr, GST_TYPE_LIST);
1155 g_value_init (&item, G_TYPE_INT);
1156 g_value_set_int (&item, 2);
1157 gst_value_list_append_value (&arr, &item);
1158 g_value_set_int (&item, 4);
1159 gst_value_list_append_value (&arr, &item);
1160 g_value_unset (&item);
1162 gst_caps_set_value (caps, "mpegversion", &arr);
1163 g_value_unset (&arr);
1165 g_value_init (&arr, GST_TYPE_LIST);
1166 g_value_init (&item, G_TYPE_STRING);
1167 g_value_set_string (&item, "raw");
1168 gst_value_list_append_value (&arr, &item);
1169 g_value_set_string (&item, "adts");
1170 gst_value_list_append_value (&arr, &item);
1171 g_value_set_string (&item, "adif");
1172 gst_value_list_append_value (&arr, &item);
1173 g_value_unset (&item);
1175 gst_caps_set_value (caps, "stream-format", &arr);
1176 g_value_unset (&arr);
1178 gst_caps_set_simple (caps, "mpegversion", G_TYPE_INT, 4,
1179 "stream-format", G_TYPE_STRING, "raw",
1180 "base-profile", G_TYPE_STRING, "lc", NULL);
1182 if (ctx && ctx->codecdata_size > 0) {
1183 gst_codec_utils_aac_caps_set_level_and_profile (caps,
1184 ctx->codecdata, ctx->codecdata_size);
1187 } else if (strcmp (name, "ac3") == 0) {
1188 caps = gst_maru_audio_caps_new (ctx, name, "audio/x-ac3", NULL);
1189 } else if (strcmp (name, "mp3") == 0) {
1191 caps = gst_maru_audio_caps_new (ctx, name, "audio/mpeg",
1192 "mpegversion", G_TYPE_INT, 1,
1193 "layer", GST_TYPE_INT_RANGE, 1, 3, NULL);
1195 caps = gst_caps_new_simple("audio/mpeg",
1196 "mpegversion", G_TYPE_INT, 1,
1197 "layer", GST_TYPE_INT_RANGE, 1, 3, NULL);
1199 } else if (strcmp (name, "mp3adu") == 0) {
1202 mime_type = g_strdup_printf ("audio/x-gst_ff-%s", name);
1203 caps = gst_maru_audio_caps_new (ctx, name, mime_type, NULL);
1208 } else if (g_str_has_prefix(name, "wmav")) {
1210 if (strcmp (name, "wmav2") == 0) {
1213 caps = gst_maru_audio_caps_new (ctx, name, "audio/x-wma", "wmaversion",
1214 G_TYPE_INT, version, "block_align", GST_TYPE_INT_RANGE, 0, G_MAXINT,
1215 "bitrate", GST_TYPE_INT_RANGE, 0, G_MAXINT, NULL);
1217 GST_ERROR("failed to new caps for %s", name);
1221 if (ctx && ctx->codecdata_size > 0) {
1222 GstBuffer *data = gst_buffer_new_and_alloc (ctx->codecdata_size);
1224 memcpy (GST_BUFFER_DATA(data), ctx->codecdata, ctx->codecdata_size);
1225 gst_caps_set_simple (caps, "codec_data", GST_TYPE_BUFFER, data, NULL);
1227 gst_buffer_unref (data);
1229 GST_LOG ("caps for codec %s %" GST_PTR_FORMAT, name, caps);
1231 GST_LOG ("No caps found for codec %s", name);
1237 typedef struct PixFmtInfo
1239 uint8_t x_chroma_shift; /* X chroma subsampling factor is 2 ^ shift */
1240 uint8_t y_chroma_shift; /* Y chroma subsampling factor is 2 ^ shift */
1243 static PixFmtInfo pix_fmt_info[PIX_FMT_NB];
1246 gst_maru_init_pix_fmt_info (void)
1248 pix_fmt_info[PIX_FMT_YUV420P].x_chroma_shift = 1,
1249 pix_fmt_info[PIX_FMT_YUV420P].y_chroma_shift = 1;
1251 pix_fmt_info[PIX_FMT_YUV422P].x_chroma_shift = 1;
1252 pix_fmt_info[PIX_FMT_YUV422P].y_chroma_shift = 0;
1254 pix_fmt_info[PIX_FMT_YUV444P].x_chroma_shift = 0;
1255 pix_fmt_info[PIX_FMT_YUV444P].y_chroma_shift = 0;
1257 pix_fmt_info[PIX_FMT_YUYV422].x_chroma_shift = 1;
1258 pix_fmt_info[PIX_FMT_YUYV422].y_chroma_shift = 0;
1260 pix_fmt_info[PIX_FMT_YUV410P].x_chroma_shift = 2;
1261 pix_fmt_info[PIX_FMT_YUV410P].y_chroma_shift = 2;
1263 pix_fmt_info[PIX_FMT_YUV411P].x_chroma_shift = 2;
1264 pix_fmt_info[PIX_FMT_YUV411P].y_chroma_shift = 0;
1267 pix_fmt_info[PIX_FMT_RGB24].x_chroma_shift = 0;
1268 pix_fmt_info[PIX_FMT_RGB24].y_chroma_shift = 0;
1270 pix_fmt_info[PIX_FMT_BGR24].x_chroma_shift = 0;
1271 pix_fmt_info[PIX_FMT_BGR24].y_chroma_shift = 0;
1273 pix_fmt_info[PIX_FMT_RGB32].x_chroma_shift = 0;
1274 pix_fmt_info[PIX_FMT_RGB32].y_chroma_shift = 0;
1276 pix_fmt_info[PIX_FMT_RGB565].x_chroma_shift = 0;
1277 pix_fmt_info[PIX_FMT_RGB565].y_chroma_shift = 0;
1279 pix_fmt_info[PIX_FMT_RGB555].x_chroma_shift = 0;
1280 pix_fmt_info[PIX_FMT_RGB555].y_chroma_shift = 0;
1284 gst_maru_avpicture_size (int pix_fmt, int width, int height)
1286 int size, w2, h2, size2;
1287 int stride, stride2;
1291 pinfo = &pix_fmt_info[pix_fmt];
1294 case PIX_FMT_YUV420P:
1295 case PIX_FMT_YUV422P:
1296 case PIX_FMT_YUV444P:
1297 case PIX_FMT_YUV410P:
1298 case PIX_FMT_YUV411P:
1299 stride = ROUND_UP_4(width);
1300 h2 = ROUND_UP_X(height, pinfo->y_chroma_shift);
1302 w2 = DIV_ROUND_UP_X(width, pinfo->x_chroma_shift);
1303 stride2 = ROUND_UP_4(w2);
1304 h2 = DIV_ROUND_UP_X(height, pinfo->y_chroma_shift);
1305 size2 = stride2 * h2;
1306 fsize = size + 2 * size2;
1310 stride = ROUND_UP_4 (width * 3);
1311 fsize = stride * height;
1315 fsize = stride * height;
1317 case PIX_FMT_RGB555:
1318 case PIX_FMT_RGB565:
1319 stride = ROUND_UP_4 (width * 2);
1320 fsize = stride * height;
1331 gst_maru_align_size (int buf_size)
1335 align_size = buf_size / 1024;
1337 for (i = 0; i < 14; i++) {
1338 if (align_size < (1 << i)) {
1339 align_size = 1024 * (1 << i);