summaryrefslogtreecommitdiff
path: root/gst-libs/gst/codecparsers/gsth264meta.c
blob: a459b6f914de62975c4e95c210212ac7af5ad309 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * GStreamer
 * Copyright (C) 2012 Edward Hervey <edward@collabora.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>

#include "gsth264meta.h"

GST_DEBUG_CATEGORY (h264_meta_debug);
#define GST_CAT_DEFAULT h264_meta_debug

static gboolean
gst_h264_meta_init (GstH264Meta * h264_meta,
    gpointer params, GstBuffer * buffer)
{
  h264_meta->sps = NULL;
  h264_meta->pps = NULL;
  h264_meta->sei = NULL;

  h264_meta->num_slices = 0;
  memset (&h264_meta->slices, 0, GST_MAX_H264_SLICE * sizeof (GstH264SliceHdr));
  memset (&h264_meta->slice_offsets, 0, GST_MAX_H264_SLICE * sizeof (gsize));

  return TRUE;
}

static void
gst_h264_meta_free (GstH264Meta * h264_meta, GstBuffer * buffer)
{
  GList *tmp;

  if (h264_meta->sps) {
    for (tmp = h264_meta->sps; tmp; tmp = tmp->next) {
      GstH264SPS *sps = (GstH264SPS *) tmp->data;
      g_slice_free (GstH264SPS, sps);
    }
    g_list_free (h264_meta->sps);
  }

  if (h264_meta->pps) {
    for (tmp = h264_meta->pps; tmp; tmp = tmp->next) {
      GstH264PPS *pps = (GstH264PPS *) tmp->data;
      g_slice_free (GstH264PPS, pps);
    }
    g_list_free (h264_meta->pps);
  }

  if (h264_meta->sei) {
    for (tmp = h264_meta->sei; tmp; tmp = tmp->next) {
      GstH264SEIMessage *sei = (GstH264SEIMessage *) tmp->data;
      g_slice_free (GstH264SEIMessage, sei);
    }
    g_list_free (h264_meta->sei);
  }
}

GType
gst_h264_meta_api_get_type (void)
{
  static volatile GType type;
  static const gchar *tags[] = { "memory", NULL };      /* don't know what to set here */

  if (g_once_init_enter (&type)) {
    GType _type = gst_meta_api_type_register ("GstH264MetaAPI", tags);
    GST_DEBUG_CATEGORY_INIT (h264_meta_debug, "h264meta", 0,
        "H264 video GstMeta");

    g_once_init_leave (&type, _type);
  }
  return type;
}

const GstMetaInfo *
gst_h264_meta_get_info (void)
{
  static const GstMetaInfo *h264_meta_info = NULL;

  if (h264_meta_info == NULL) {
    h264_meta_info =
        gst_meta_register (GST_H264_META_API_TYPE,
        "GstH264Meta", sizeof (GstH264Meta),
        (GstMetaInitFunction) gst_h264_meta_init,
        (GstMetaFreeFunction) gst_h264_meta_free,
        (GstMetaTransformFunction) NULL);
  }
  return h264_meta_info;
}

/**
 * gst_buffer_add_h264_meta:
 * @buffer: a #GstBuffer
 *
 * Creates and adds a #GstH264Meta to a @buffer.
 *
 * Provided structures must either be %NULL or GSlice-allocated.
 *
 * Returns: (transfer full): a newly created #GstH264Meta
 */
GstH264Meta *
gst_buffer_add_h264_meta (GstBuffer * buffer)
{
  GstH264Meta *h264_meta;

  h264_meta =
      (GstH264Meta *) gst_buffer_add_meta (buffer, GST_H264_META_INFO, NULL);
  g_assert (h264_meta);

  return h264_meta;
}

void
gst_h264_meta_add_sps (GstH264Meta * meta, const GstH264SPS * sps)
{
  GST_DEBUG ("Adding SPS %d", sps->id);
  meta->sps = g_list_append (meta->sps, g_slice_dup (GstH264SPS, sps));
}

void
gst_h264_meta_add_pps (GstH264Meta * meta, const GstH264PPS * pps)
{
  GST_DEBUG ("Adding PPS %d", pps->id);
  meta->pps = g_list_append (meta->pps, g_slice_dup (GstH264PPS, pps));
}

void
gst_h264_meta_add_sei (GstH264Meta * meta, const GstH264SEIMessage * sei)
{
  GST_DEBUG ("Adding SEI type %u", sei->payloadType);

  meta->sei = g_list_append (meta->sei, g_slice_dup (GstH264SEIMessage, sei));
}

void
gst_h264_meta_add_slice (GstH264Meta * meta, const GstH264SliceHdr * slice,
    gsize offset)
{
  GST_DEBUG ("Adding slice, type:%d, first_mb_in_slice:%d, offset:%"
      G_GSIZE_FORMAT, slice->type, slice->first_mb_in_slice, offset);

  g_return_if_fail (meta->num_slices < 16);

  meta->slice_offsets[meta->num_slices] = offset;
  meta->slices[meta->num_slices] = *slice;

  meta->num_slices++;
}