• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer MXF Apple ProRes Mapping
2  * Copyright (C) 2019 Tim-Philipp Müller <tim@centricular.com>
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 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22 
23 #include <gst/gst.h>
24 #include <gst/video/video.h>
25 #include <string.h>
26 
27 #include "mxfprores.h"
28 #include "mxfessence.h"
29 
30 GST_DEBUG_CATEGORY_EXTERN (mxf_debug);
31 #define GST_CAT_DEFAULT mxf_debug
32 
33 /* Implementation of SMPTE RDD 44:2017-11 "Material Exchange Format - Mapping
34  * and Application of Apple ProRes"
35  */
36 static gboolean
mxf_is_prores_essence_track(const MXFMetadataTimelineTrack * track)37 mxf_is_prores_essence_track (const MXFMetadataTimelineTrack * track)
38 {
39   guint i;
40 
41   g_return_val_if_fail (track != NULL, FALSE);
42 
43   if (track->parent.descriptor == NULL)
44     return FALSE;
45 
46   for (i = 0; i < track->parent.n_descriptor; i++) {
47     MXFMetadataFileDescriptor *d = track->parent.descriptor[i];
48     MXFUL *key;
49 
50     if (!d)
51       continue;
52 
53     key = &d->essence_container;
54     if (mxf_is_generic_container_essence_container_label (key) &&
55         key->u[12] == 0x02 && key->u[13] == 0x1C) {
56       return TRUE;
57     }
58   }
59 
60   return FALSE;
61 }
62 
63 static GstFlowReturn
mxf_prores_handle_essence_element(const MXFUL * key,GstBuffer * buffer,GstCaps * caps,MXFMetadataTimelineTrack * track,gpointer mapping_data,GstBuffer ** outbuf)64 mxf_prores_handle_essence_element (const MXFUL * key, GstBuffer * buffer,
65     GstCaps * caps,
66     MXFMetadataTimelineTrack * track,
67     gpointer mapping_data, GstBuffer ** outbuf)
68 {
69   *outbuf = buffer;
70 
71   /* SMPTE RDD 41:2017-11 6.3 */
72   if (key->u[12] != 0x15 || key->u[14] != 0x17) {
73     GST_MEMDUMP ("Essence element", &key->u[0], 16);
74     GST_ERROR ("Invalid ProRes essence element");
75     return GST_FLOW_ERROR;
76   }
77 
78   return GST_FLOW_OK;
79 }
80 
81 static MXFEssenceWrapping
mxf_prores_get_track_wrapping(const MXFMetadataTimelineTrack * track)82 mxf_prores_get_track_wrapping (const MXFMetadataTimelineTrack * track)
83 {
84   guint i;
85 
86   g_return_val_if_fail (track != NULL, MXF_ESSENCE_WRAPPING_CUSTOM_WRAPPING);
87 
88   if (track->parent.descriptor == NULL) {
89     GST_ERROR ("No descriptor found for this track");
90     return MXF_ESSENCE_WRAPPING_CUSTOM_WRAPPING;
91   }
92 
93   for (i = 0; i < track->parent.n_descriptor; i++) {
94     if (!track->parent.descriptor[i])
95       continue;
96 
97     if (!MXF_IS_METADATA_GENERIC_PICTURE_ESSENCE_DESCRIPTOR (track->
98             parent.descriptor[i]))
99       continue;
100 
101     /* sanity check */
102     if (track->parent.descriptor[i]->essence_container.u[13] != 0x1C)
103       return MXF_ESSENCE_WRAPPING_CUSTOM_WRAPPING;
104 
105     switch (track->parent.descriptor[i]->essence_container.u[14]) {
106       case 0x01:
107         return MXF_ESSENCE_WRAPPING_FRAME_WRAPPING;
108       case 0x02:
109         return MXF_ESSENCE_WRAPPING_CLIP_WRAPPING;
110       default:
111         return MXF_ESSENCE_WRAPPING_CUSTOM_WRAPPING;
112     }
113   }
114 
115   return MXF_ESSENCE_WRAPPING_CUSTOM_WRAPPING;
116 }
117 
118 static GstCaps *
mxf_prores_create_caps(MXFMetadataTimelineTrack * track,GstTagList ** tags,gboolean * intra_only,MXFEssenceElementHandleFunc * handler,gpointer * mapping_data)119 mxf_prores_create_caps (MXFMetadataTimelineTrack * track, GstTagList ** tags,
120     gboolean * intra_only, MXFEssenceElementHandleFunc * handler,
121     gpointer * mapping_data)
122 {
123   MXFMetadataGenericPictureEssenceDescriptor *d = NULL;
124   const gchar *variant;
125   GstCaps *caps;
126   guint i;
127 
128   g_return_val_if_fail (track != NULL, NULL);
129 
130   if (track->parent.descriptor == NULL) {
131     GST_ERROR ("No descriptor found for this track");
132     return NULL;
133   }
134 
135   for (i = 0; i < track->parent.n_descriptor; i++) {
136     if (MXF_IS_METADATA_GENERIC_PICTURE_ESSENCE_DESCRIPTOR (track->
137             parent.descriptor[i])) {
138       d = MXF_METADATA_GENERIC_PICTURE_ESSENCE_DESCRIPTOR (track->
139           parent.descriptor[i]);
140       break;
141     }
142   }
143 
144   if (d == NULL) {
145     GST_ERROR ("No picture essence coding descriptor found for this track");
146     return NULL;
147   }
148 
149   if (d->picture_essence_coding.u[13] != 0x06) {
150     GST_MEMDUMP ("Picture essence", &d->picture_essence_coding.u[0], 16);
151     GST_ERROR ("Picture essence coding descriptor not for ProRes?!");
152     return NULL;
153   }
154 
155   GST_INFO ("Found Apple ProRes video stream");
156 
157   switch (d->picture_essence_coding.u[14]) {
158     case 0x01:
159       variant = "proxy";
160       break;
161     case 0x02:
162       variant = "lt";
163       break;
164     case 0x03:
165       variant = "standard";
166       break;
167     case 0x04:
168       variant = "hq";
169       break;
170     case 0x05:
171       variant = "4444";
172       break;
173     case 0x06:
174       variant = "4444xq";
175       break;
176     default:
177       GST_ERROR ("Unknown ProRes profile %2d", d->picture_essence_coding.u[14]);
178       return NULL;
179   }
180 
181   *handler = mxf_prores_handle_essence_element;
182 
183   caps = gst_caps_new_simple ("video/x-prores",
184       "variant", G_TYPE_STRING, variant, NULL);
185 
186   if (d)
187     mxf_metadata_generic_picture_essence_descriptor_set_caps (d, caps);
188 
189   if (!*tags)
190     *tags = gst_tag_list_new_empty ();
191 
192   gst_tag_list_add (*tags, GST_TAG_MERGE_APPEND, GST_TAG_CODEC, "Apple ProRes",
193       NULL);
194 
195   *intra_only = TRUE;
196 
197   return caps;
198 }
199 
200 static const MXFEssenceElementHandler mxf_prores_essence_element_handler = {
201   mxf_is_prores_essence_track,
202   mxf_prores_get_track_wrapping,
203   mxf_prores_create_caps
204 };
205 
206 void
mxf_prores_init(void)207 mxf_prores_init (void)
208 {
209   mxf_essence_element_handler_register (&mxf_prores_essence_element_handler);
210 }
211