1 /* GStreamer
2 * Copyright (C) 2008-2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 /* Implementation of SMPTE S2019-4 - Mapping VC-3 coding units into the MXF
21 * Generic Container
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <gst/gst.h>
29 #include <gst/video/video.h>
30 #include <string.h>
31
32 #include "mxfvc3.h"
33 #include "mxfessence.h"
34
35 GST_DEBUG_CATEGORY_EXTERN (mxf_debug);
36 #define GST_CAT_DEFAULT mxf_debug
37
38 static const MXFUL picture_essence_coding_vc3_avid = { {
39 0x06, 0x0e, 0x2b, 0x34, 0x04, 0x01, 0x01, 0x01, 0x0e, 0x04, 0x02, 0x01,
40 0x02,
41 0x04, 0x01, 0x00}
42 };
43
44 static gboolean
mxf_is_vc3_essence_track(const MXFMetadataTimelineTrack * track)45 mxf_is_vc3_essence_track (const MXFMetadataTimelineTrack * track)
46 {
47 guint i;
48
49 g_return_val_if_fail (track != NULL, FALSE);
50
51 if (track->parent.descriptor == NULL)
52 return FALSE;
53
54 for (i = 0; i < track->parent.n_descriptor; i++) {
55 MXFMetadataFileDescriptor *d = track->parent.descriptor[i];
56 MXFUL *key;
57
58 if (!d)
59 continue;
60
61 key = &d->essence_container;
62 /* SMPTE S2019-4 7 */
63 if (mxf_is_generic_container_essence_container_label (key) &&
64 key->u[12] == 0x02 && key->u[13] == 0x11 &&
65 (key->u[14] == 0x01 || key->u[14] == 0x02)) {
66 return TRUE;
67 } else if (mxf_is_avid_essence_container_label (key)) {
68 MXFMetadataGenericPictureEssenceDescriptor *p;
69
70 if (!MXF_IS_METADATA_GENERIC_PICTURE_ESSENCE_DESCRIPTOR (d))
71 return FALSE;
72 p = MXF_METADATA_GENERIC_PICTURE_ESSENCE_DESCRIPTOR (d);
73
74 key = &p->picture_essence_coding;
75 if (mxf_ul_is_subclass (&picture_essence_coding_vc3_avid, key))
76 return TRUE;
77 }
78 }
79
80 return FALSE;
81 }
82
83 static GstFlowReturn
mxf_vc3_handle_essence_element(const MXFUL * key,GstBuffer * buffer,GstCaps * caps,MXFMetadataTimelineTrack * track,gpointer mapping_data,GstBuffer ** outbuf)84 mxf_vc3_handle_essence_element (const MXFUL * key, GstBuffer * buffer,
85 GstCaps * caps,
86 MXFMetadataTimelineTrack * track,
87 gpointer mapping_data, GstBuffer ** outbuf)
88 {
89 *outbuf = buffer;
90
91 /* SMPTE 2019-4 6.1 */
92 if (key->u[12] != 0x15 || (key->u[14] != 0x05 && key->u[14] != 0x06
93 && key->u[14] != 0x0C && key->u[14] != 0x0D)) {
94 GST_ERROR ("Invalid VC-3 essence element");
95 return GST_FLOW_ERROR;
96 }
97
98 return GST_FLOW_OK;
99 }
100
101 static MXFEssenceWrapping
mxf_vc3_get_track_wrapping(const MXFMetadataTimelineTrack * track)102 mxf_vc3_get_track_wrapping (const MXFMetadataTimelineTrack * track)
103 {
104 guint i;
105
106 g_return_val_if_fail (track != NULL, MXF_ESSENCE_WRAPPING_CUSTOM_WRAPPING);
107
108 if (track->parent.descriptor == NULL) {
109 GST_ERROR ("No descriptor found for this track");
110 return MXF_ESSENCE_WRAPPING_CUSTOM_WRAPPING;
111 }
112
113 for (i = 0; i < track->parent.n_descriptor; i++) {
114 if (!track->parent.descriptor[i])
115 continue;
116
117 if (!MXF_IS_METADATA_GENERIC_PICTURE_ESSENCE_DESCRIPTOR (track->
118 parent.descriptor[i])
119 && !(MXF_IS_METADATA_FILE_DESCRIPTOR (track->parent.descriptor[i])
120 && !MXF_IS_METADATA_MULTIPLE_DESCRIPTOR (track->
121 parent.descriptor[i])))
122 continue;
123
124 switch (track->parent.descriptor[i]->essence_container.u[14]) {
125 case 0x01:
126 return MXF_ESSENCE_WRAPPING_FRAME_WRAPPING;
127 break;
128 case 0x02:
129 return MXF_ESSENCE_WRAPPING_CLIP_WRAPPING;
130 break;
131 default:
132 return MXF_ESSENCE_WRAPPING_CUSTOM_WRAPPING;
133 break;
134 }
135 }
136
137 return MXF_ESSENCE_WRAPPING_CUSTOM_WRAPPING;
138 }
139
140 static GstCaps *
mxf_vc3_create_caps(MXFMetadataTimelineTrack * track,GstTagList ** tags,gboolean * intra_only,MXFEssenceElementHandleFunc * handler,gpointer * mapping_data)141 mxf_vc3_create_caps (MXFMetadataTimelineTrack * track, GstTagList ** tags,
142 gboolean * intra_only, MXFEssenceElementHandleFunc * handler,
143 gpointer * mapping_data)
144 {
145 MXFMetadataFileDescriptor *f = NULL;
146 MXFMetadataGenericPictureEssenceDescriptor *p = NULL;
147 guint i;
148 GstCaps *caps = NULL;
149
150 g_return_val_if_fail (track != NULL, NULL);
151
152 if (track->parent.descriptor == NULL) {
153 GST_ERROR ("No descriptor found for this track");
154 return NULL;
155 }
156
157 for (i = 0; i < track->parent.n_descriptor; i++) {
158 if (!track->parent.descriptor[i])
159 continue;
160
161 if (MXF_IS_METADATA_GENERIC_PICTURE_ESSENCE_DESCRIPTOR (track->
162 parent.descriptor[i])) {
163 p = (MXFMetadataGenericPictureEssenceDescriptor *) track->parent.
164 descriptor[i];
165 f = track->parent.descriptor[i];
166 break;
167 } else if (MXF_IS_METADATA_FILE_DESCRIPTOR (track->parent.descriptor[i]) &&
168 !MXF_IS_METADATA_MULTIPLE_DESCRIPTOR (track->parent.descriptor[i])) {
169 f = track->parent.descriptor[i];
170 }
171 }
172
173 if (!f) {
174 GST_ERROR ("No descriptor found for this track");
175 return NULL;
176 }
177
178 *handler = mxf_vc3_handle_essence_element;
179
180 caps = gst_caps_new_empty_simple ("video/x-dnxhd");
181 if (p) {
182 mxf_metadata_generic_picture_essence_descriptor_set_caps (p, caps);
183 } else {
184 GST_WARNING ("Only a generic file descriptor found");
185 }
186
187 if (!*tags)
188 *tags = gst_tag_list_new_empty ();
189 gst_tag_list_add (*tags, GST_TAG_MERGE_APPEND, GST_TAG_VIDEO_CODEC,
190 "VC-3 Video", NULL);
191 *intra_only = TRUE;
192
193 return caps;
194 }
195
196 static const MXFEssenceElementHandler mxf_vc3_essence_element_handler = {
197 mxf_is_vc3_essence_track,
198 mxf_vc3_get_track_wrapping,
199 mxf_vc3_create_caps
200 };
201
202 static GstFlowReturn
mxf_vc3_write_func(GstBuffer * buffer,gpointer mapping_data,GstAdapter * adapter,GstBuffer ** outbuf,gboolean flush)203 mxf_vc3_write_func (GstBuffer * buffer, gpointer mapping_data,
204 GstAdapter * adapter, GstBuffer ** outbuf, gboolean flush)
205 {
206 *outbuf = buffer;
207 return GST_FLOW_OK;
208 }
209
210 /* FIXME: In which version was this added? Byte 7, assuming version 10 */
211 static const guint8 vc3_essence_container_ul[] = {
212 0x06, 0x0e, 0x2b, 0x34, 0x04, 0x01, 0x01, 0x0A,
213 0x0d, 0x01, 0x03, 0x01, 0x02, 0x11, 0x01, 0x00
214 };
215
216 static MXFMetadataFileDescriptor *
mxf_vc3_get_descriptor(GstPadTemplate * tmpl,GstCaps * caps,MXFEssenceElementWriteFunc * handler,gpointer * mapping_data)217 mxf_vc3_get_descriptor (GstPadTemplate * tmpl, GstCaps * caps,
218 MXFEssenceElementWriteFunc * handler, gpointer * mapping_data)
219 {
220 MXFMetadataCDCIPictureEssenceDescriptor *ret;
221 GstStructure *s;
222
223 s = gst_caps_get_structure (caps, 0);
224 if (strcmp (gst_structure_get_name (s), "video/x-dnxhd") != 0) {
225 GST_ERROR ("Invalid caps %" GST_PTR_FORMAT, caps);
226 return NULL;
227 }
228
229 ret = (MXFMetadataCDCIPictureEssenceDescriptor *)
230 g_object_new (MXF_TYPE_METADATA_CDCI_PICTURE_ESSENCE_DESCRIPTOR, NULL);
231
232 memcpy (&ret->parent.parent.essence_container, &vc3_essence_container_ul, 16);
233
234 if (!mxf_metadata_generic_picture_essence_descriptor_from_caps (&ret->parent,
235 caps)) {
236 g_object_unref (ret);
237 return NULL;
238 }
239
240 *handler = mxf_vc3_write_func;
241
242 return (MXFMetadataFileDescriptor *) ret;
243 }
244
245 static void
mxf_vc3_update_descriptor(MXFMetadataFileDescriptor * d,GstCaps * caps,gpointer mapping_data,GstBuffer * buf)246 mxf_vc3_update_descriptor (MXFMetadataFileDescriptor * d, GstCaps * caps,
247 gpointer mapping_data, GstBuffer * buf)
248 {
249 return;
250 }
251
252 static void
mxf_vc3_get_edit_rate(MXFMetadataFileDescriptor * a,GstCaps * caps,gpointer mapping_data,GstBuffer * buf,MXFMetadataSourcePackage * package,MXFMetadataTimelineTrack * track,MXFFraction * edit_rate)253 mxf_vc3_get_edit_rate (MXFMetadataFileDescriptor * a, GstCaps * caps,
254 gpointer mapping_data, GstBuffer * buf, MXFMetadataSourcePackage * package,
255 MXFMetadataTimelineTrack * track, MXFFraction * edit_rate)
256 {
257 edit_rate->n = a->sample_rate.n;
258 edit_rate->d = a->sample_rate.d;
259 }
260
261 static guint32
mxf_vc3_get_track_number_template(MXFMetadataFileDescriptor * a,GstCaps * caps,gpointer mapping_data)262 mxf_vc3_get_track_number_template (MXFMetadataFileDescriptor * a,
263 GstCaps * caps, gpointer mapping_data)
264 {
265 return (0x15 << 24) | (0x0C << 8);
266 }
267
268 static MXFEssenceElementWriter mxf_vc3_essence_element_writer = {
269 mxf_vc3_get_descriptor,
270 mxf_vc3_update_descriptor,
271 mxf_vc3_get_edit_rate,
272 mxf_vc3_get_track_number_template,
273 NULL,
274 {{0,}}
275 };
276
277 void
mxf_vc3_init(void)278 mxf_vc3_init (void)
279 {
280 mxf_essence_element_handler_register (&mxf_vc3_essence_element_handler);
281
282 mxf_vc3_essence_element_writer.pad_template =
283 gst_pad_template_new ("vc3_video_sink_%u", GST_PAD_SINK, GST_PAD_REQUEST,
284 gst_caps_from_string ("video/x-dnxhd, width = " GST_VIDEO_SIZE_RANGE
285 ", height = " GST_VIDEO_SIZE_RANGE ", framerate = "
286 GST_VIDEO_FPS_RANGE));
287 memcpy (&mxf_vc3_essence_element_writer.data_definition,
288 mxf_metadata_track_identifier_get (MXF_METADATA_TRACK_PICTURE_ESSENCE),
289 16);
290 mxf_essence_element_writer_register (&mxf_vc3_essence_element_writer);
291 }
292