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 383M - Mapping DV-DIF data into the MXF
21 * Generic Container
22 */
23
24 /* TODO:
25 * - playbin hangs on a lot of MXF/DV-DIF files (bug #563827)
26 * - decodebin creates loops inside the linking graph (bug #563828)
27 * - track descriptor might be multiple descriptor, one for sound, one for video
28 * - there might be 2 tracks for one essence, i.e. one audio/one video track
29 */
30
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include <gst/gst.h>
36 #include <gst/video/video.h>
37 #include <string.h>
38
39 #include "mxfdv-dif.h"
40 #include "mxfessence.h"
41
42 GST_DEBUG_CATEGORY_EXTERN (mxf_debug);
43 #define GST_CAT_DEFAULT mxf_debug
44
45 static const MXFUL picture_essence_coding_dv = { {
46 0x06, 0x0e, 0x2b, 0x34, 0x04, 0x01, 0x01, 0x01, 0x04, 0x01, 0x02, 0x02,
47 0x02}
48 };
49
50 static gboolean
mxf_is_dv_dif_essence_track(const MXFMetadataTimelineTrack * track)51 mxf_is_dv_dif_essence_track (const MXFMetadataTimelineTrack * track)
52 {
53 guint i;
54
55 g_return_val_if_fail (track != NULL, FALSE);
56
57 if (track->parent.descriptor == NULL)
58 return FALSE;
59
60 for (i = 0; i < track->parent.n_descriptor; i++) {
61 MXFMetadataFileDescriptor *d = track->parent.descriptor[i];
62 MXFUL *key;
63
64 if (!d)
65 continue;
66
67 key = &d->essence_container;
68 /* SMPTE 383M 8 */
69 if (mxf_is_generic_container_essence_container_label (key) &&
70 key->u[12] == 0x02 && key->u[13] == 0x02) {
71 return TRUE;
72 } else if (mxf_is_avid_essence_container_label (key)) {
73 MXFMetadataGenericPictureEssenceDescriptor *p;
74
75 if (!MXF_IS_METADATA_GENERIC_PICTURE_ESSENCE_DESCRIPTOR (d))
76 return FALSE;
77 p = MXF_METADATA_GENERIC_PICTURE_ESSENCE_DESCRIPTOR (d);
78
79 key = &p->picture_essence_coding;
80 if (mxf_ul_is_subclass (&picture_essence_coding_dv, key))
81 return TRUE;
82 }
83 }
84
85 return FALSE;
86 }
87
88 static GstFlowReturn
mxf_dv_dif_handle_essence_element(const MXFUL * key,GstBuffer * buffer,GstCaps * caps,MXFMetadataTimelineTrack * track,gpointer mapping_data,GstBuffer ** outbuf)89 mxf_dv_dif_handle_essence_element (const MXFUL * key, GstBuffer * buffer,
90 GstCaps * caps,
91 MXFMetadataTimelineTrack * track,
92 gpointer mapping_data, GstBuffer ** outbuf)
93 {
94 *outbuf = buffer;
95
96 /* SMPTE 383 6.1.1 */
97 if (key->u[12] != 0x18 || (key->u[14] != 0x01 && key->u[14] != 0x02)) {
98 GST_ERROR ("Invalid DV-DIF essence element");
99 return GST_FLOW_ERROR;
100 }
101
102 return GST_FLOW_OK;
103 }
104
105 static MXFEssenceWrapping
mxf_dv_dif_get_track_wrapping(const MXFMetadataTimelineTrack * track)106 mxf_dv_dif_get_track_wrapping (const MXFMetadataTimelineTrack * track)
107 {
108 guint i;
109
110 g_return_val_if_fail (track != NULL, MXF_ESSENCE_WRAPPING_CUSTOM_WRAPPING);
111
112 if (track->parent.descriptor == NULL) {
113 GST_ERROR ("No descriptor found for this track");
114 return MXF_ESSENCE_WRAPPING_CUSTOM_WRAPPING;
115 }
116
117 for (i = 0; i < track->parent.n_descriptor; i++) {
118 if (!track->parent.descriptor[i])
119 continue;
120
121 if (!MXF_IS_METADATA_GENERIC_PICTURE_ESSENCE_DESCRIPTOR (track->
122 parent.descriptor[i]))
123 continue;
124
125 switch (track->parent.descriptor[i]->essence_container.u[15]) {
126 case 0x01:
127 return MXF_ESSENCE_WRAPPING_FRAME_WRAPPING;
128 break;
129 case 0x02:
130 return MXF_ESSENCE_WRAPPING_CLIP_WRAPPING;
131 break;
132 default:
133 return MXF_ESSENCE_WRAPPING_CUSTOM_WRAPPING;
134 break;
135 }
136 }
137
138 return MXF_ESSENCE_WRAPPING_CUSTOM_WRAPPING;
139 }
140
141 static GstCaps *
mxf_dv_dif_create_caps(MXFMetadataTimelineTrack * track,GstTagList ** tags,gboolean * intra_only,MXFEssenceElementHandleFunc * handler,gpointer * mapping_data)142 mxf_dv_dif_create_caps (MXFMetadataTimelineTrack * track, GstTagList ** tags,
143 gboolean * intra_only, MXFEssenceElementHandleFunc * handler,
144 gpointer * mapping_data)
145 {
146 GstCaps *caps = NULL;
147 guint i;
148 MXFMetadataGenericPictureEssenceDescriptor *d = 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 (MXF_IS_METADATA_GENERIC_PICTURE_ESSENCE_DESCRIPTOR (track->
159 parent.descriptor[i])) {
160 d = MXF_METADATA_GENERIC_PICTURE_ESSENCE_DESCRIPTOR (track->
161 parent.descriptor[i]);
162 break;
163 }
164 }
165
166 *handler = mxf_dv_dif_handle_essence_element;
167 /* SMPTE 383M 8 */
168
169 /* TODO: might be video or audio only, use values of the generic sound/picture
170 * descriptor in the caps in that case
171 */
172 GST_DEBUG ("Found DV-DIF stream");
173 caps =
174 gst_caps_new_simple ("video/x-dv", "systemstream", G_TYPE_BOOLEAN, TRUE,
175 NULL);
176
177 if (d)
178 mxf_metadata_generic_picture_essence_descriptor_set_caps (d, caps);
179
180 if (!*tags)
181 *tags = gst_tag_list_new_empty ();
182
183 gst_tag_list_add (*tags, GST_TAG_MERGE_APPEND, GST_TAG_CODEC, "DV-DIF", NULL);
184
185 *intra_only = TRUE;
186
187 return caps;
188 }
189
190 static const MXFEssenceElementHandler mxf_dv_dif_essence_element_handler = {
191 mxf_is_dv_dif_essence_track,
192 mxf_dv_dif_get_track_wrapping,
193 mxf_dv_dif_create_caps
194 };
195
196 static GstFlowReturn
mxf_dv_dif_write_func(GstBuffer * buffer,gpointer mapping_data,GstAdapter * adapter,GstBuffer ** outbuf,gboolean flush)197 mxf_dv_dif_write_func (GstBuffer * buffer,
198 gpointer mapping_data, GstAdapter * adapter, GstBuffer ** outbuf,
199 gboolean flush)
200 {
201 *outbuf = buffer;
202 return GST_FLOW_OK;
203 }
204
205 static const guint8 dv_dif_essence_container_ul[] = {
206 0x06, 0x0e, 0x2b, 0x34, 0x04, 0x01, 0x01, 0x01,
207 0x0d, 0x01, 0x03, 0x01, 0x02, 0x02, 0x7f, 0x01
208 };
209
210 static MXFMetadataFileDescriptor *
mxf_dv_dif_get_descriptor(GstPadTemplate * tmpl,GstCaps * caps,MXFEssenceElementWriteFunc * handler,gpointer * mapping_data)211 mxf_dv_dif_get_descriptor (GstPadTemplate * tmpl, GstCaps * caps,
212 MXFEssenceElementWriteFunc * handler, gpointer * mapping_data)
213 {
214 MXFMetadataCDCIPictureEssenceDescriptor *ret;
215
216 ret = (MXFMetadataCDCIPictureEssenceDescriptor *)
217 g_object_new (MXF_TYPE_METADATA_CDCI_PICTURE_ESSENCE_DESCRIPTOR, NULL);
218
219 memcpy (&ret->parent.parent.essence_container, &dv_dif_essence_container_ul,
220 16);
221
222 if (!mxf_metadata_generic_picture_essence_descriptor_from_caps (&ret->parent,
223 caps)) {
224 g_object_unref (ret);
225 return NULL;
226 }
227 *handler = mxf_dv_dif_write_func;
228
229 return (MXFMetadataFileDescriptor *) ret;
230 }
231
232 static void
mxf_dv_dif_update_descriptor(MXFMetadataFileDescriptor * d,GstCaps * caps,gpointer mapping_data,GstBuffer * buf)233 mxf_dv_dif_update_descriptor (MXFMetadataFileDescriptor * d, GstCaps * caps,
234 gpointer mapping_data, GstBuffer * buf)
235 {
236 return;
237 }
238
239 static void
mxf_dv_dif_get_edit_rate(MXFMetadataFileDescriptor * a,GstCaps * caps,gpointer mapping_data,GstBuffer * buf,MXFMetadataSourcePackage * package,MXFMetadataTimelineTrack * track,MXFFraction * edit_rate)240 mxf_dv_dif_get_edit_rate (MXFMetadataFileDescriptor * a, GstCaps * caps,
241 gpointer mapping_data, GstBuffer * buf, MXFMetadataSourcePackage * package,
242 MXFMetadataTimelineTrack * track, MXFFraction * edit_rate)
243 {
244 edit_rate->n = a->sample_rate.n;
245 edit_rate->d = a->sample_rate.d;
246 }
247
248 static guint32
mxf_dv_dif_get_track_number_template(MXFMetadataFileDescriptor * a,GstCaps * caps,gpointer mapping_data)249 mxf_dv_dif_get_track_number_template (MXFMetadataFileDescriptor * a,
250 GstCaps * caps, gpointer mapping_data)
251 {
252 return (0x18 << 24) | (0x01 << 8);
253 }
254
255 static MXFEssenceElementWriter mxf_dv_dif_essence_element_writer = {
256 mxf_dv_dif_get_descriptor,
257 mxf_dv_dif_update_descriptor,
258 mxf_dv_dif_get_edit_rate,
259 mxf_dv_dif_get_track_number_template,
260 NULL,
261 {{0,}}
262 };
263
264 void
mxf_dv_dif_init(void)265 mxf_dv_dif_init (void)
266 {
267 mxf_essence_element_handler_register (&mxf_dv_dif_essence_element_handler);
268
269 mxf_dv_dif_essence_element_writer.pad_template =
270 gst_pad_template_new ("dv_dif_video_sink_%u", GST_PAD_SINK,
271 GST_PAD_REQUEST,
272 gst_caps_from_string ("video/x-dv, width = "
273 GST_VIDEO_SIZE_RANGE ", height = " GST_VIDEO_SIZE_RANGE
274 ", framerate = " GST_VIDEO_FPS_RANGE ", systemstream = true"));
275 memcpy (&mxf_dv_dif_essence_element_writer.data_definition,
276 mxf_metadata_track_identifier_get (MXF_METADATA_TRACK_PICTURE_ESSENCE),
277 16);
278 mxf_essence_element_writer_register (&mxf_dv_dif_essence_element_writer);
279 }
280