summaryrefslogtreecommitdiff
path: root/gst/mxf/mxfprores.c
blob: 543b48554b4589c185ec2e3e30cfeafbad2fee3d (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* GStreamer MXF Apple ProRes Mapping
 * Copyright (C) 2019 Tim-Philipp Müller <tim@centricular.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., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gst/gst.h>
#include <gst/video/video.h>
#include <string.h>

#include "mxfprores.h"
#include "mxfessence.h"

GST_DEBUG_CATEGORY_EXTERN (mxf_debug);
#define GST_CAT_DEFAULT mxf_debug

/* Implementation of SMPTE RDD 44:2017-11 "Material Exchange Format - Mapping
 * and Application of Apple ProRes"
 */
static gboolean
mxf_is_prores_essence_track (const MXFMetadataTimelineTrack * track)
{
  guint i;

  g_return_val_if_fail (track != NULL, FALSE);

  if (track->parent.descriptor == NULL)
    return FALSE;

  for (i = 0; i < track->parent.n_descriptor; i++) {
    MXFMetadataFileDescriptor *d = track->parent.descriptor[i];
    MXFUL *key;

    if (!d)
      continue;

    key = &d->essence_container;
    if (mxf_is_generic_container_essence_container_label (key) &&
        key->u[12] == 0x02 && key->u[13] == 0x1C) {
      return TRUE;
    }
  }

  return FALSE;
}

static GstFlowReturn
mxf_prores_handle_essence_element (const MXFUL * key, GstBuffer * buffer,
    GstCaps * caps,
    MXFMetadataTimelineTrack * track,
    gpointer mapping_data, GstBuffer ** outbuf)
{
  *outbuf = buffer;

  /* SMPTE RDD 41:2017-11 6.3 */
  if (key->u[12] != 0x15 || key->u[14] != 0x17) {
    GST_MEMDUMP ("Essence element", &key->u[0], 16);
    GST_ERROR ("Invalid ProRes essence element");
    return GST_FLOW_ERROR;
  }

  return GST_FLOW_OK;
}

static MXFEssenceWrapping
mxf_prores_get_track_wrapping (const MXFMetadataTimelineTrack * track)
{
  guint i;

  g_return_val_if_fail (track != NULL, MXF_ESSENCE_WRAPPING_CUSTOM_WRAPPING);

  if (track->parent.descriptor == NULL) {
    GST_ERROR ("No descriptor found for this track");
    return MXF_ESSENCE_WRAPPING_CUSTOM_WRAPPING;
  }

  for (i = 0; i < track->parent.n_descriptor; i++) {
    if (!track->parent.descriptor[i])
      continue;

    if (!MXF_IS_METADATA_GENERIC_PICTURE_ESSENCE_DESCRIPTOR (track->
            parent.descriptor[i]))
      continue;

    /* sanity check */
    if (track->parent.descriptor[i]->essence_container.u[13] != 0x1C)
      return MXF_ESSENCE_WRAPPING_CUSTOM_WRAPPING;

    switch (track->parent.descriptor[i]->essence_container.u[14]) {
      case 0x01:
        return MXF_ESSENCE_WRAPPING_FRAME_WRAPPING;
      case 0x02:
        return MXF_ESSENCE_WRAPPING_CLIP_WRAPPING;
      default:
        return MXF_ESSENCE_WRAPPING_CUSTOM_WRAPPING;
    }
  }

  return MXF_ESSENCE_WRAPPING_CUSTOM_WRAPPING;
}

static GstCaps *
mxf_prores_create_caps (MXFMetadataTimelineTrack * track, GstTagList ** tags,
    gboolean * intra_only, MXFEssenceElementHandleFunc * handler,
    gpointer * mapping_data)
{
  MXFMetadataGenericPictureEssenceDescriptor *d = NULL;
  const gchar *variant;
  GstCaps *caps;
  guint i;

  g_return_val_if_fail (track != NULL, NULL);

  if (track->parent.descriptor == NULL) {
    GST_ERROR ("No descriptor found for this track");
    return NULL;
  }

  for (i = 0; i < track->parent.n_descriptor; i++) {
    if (MXF_IS_METADATA_GENERIC_PICTURE_ESSENCE_DESCRIPTOR (track->
            parent.descriptor[i])) {
      d = MXF_METADATA_GENERIC_PICTURE_ESSENCE_DESCRIPTOR (track->
          parent.descriptor[i]);
      break;
    }
  }

  if (d == NULL) {
    GST_ERROR ("No picture essence coding descriptor found for this track");
    return NULL;
  }

  if (d->picture_essence_coding.u[13] != 0x06) {
    GST_MEMDUMP ("Picture essence", &d->picture_essence_coding.u[0], 16);
    GST_ERROR ("Picture essence coding descriptor not for ProRes?!");
    return NULL;
  }

  GST_INFO ("Found Apple ProRes video stream");

  switch (d->picture_essence_coding.u[14]) {
    case 0x01:
      variant = "proxy";
      break;
    case 0x02:
      variant = "lt";
      break;
    case 0x03:
      variant = "standard";
      break;
    case 0x04:
      variant = "hq";
      break;
    case 0x05:
      variant = "4444";
      break;
    case 0x06:
      variant = "4444xq";
      break;
    default:
      GST_ERROR ("Unknown ProRes profile %2d", d->picture_essence_coding.u[14]);
      return NULL;
  }

  *handler = mxf_prores_handle_essence_element;

  caps = gst_caps_new_simple ("video/x-prores",
      "variant", G_TYPE_STRING, variant, NULL);

  if (d)
    mxf_metadata_generic_picture_essence_descriptor_set_caps (d, caps);

  if (!*tags)
    *tags = gst_tag_list_new_empty ();

  gst_tag_list_add (*tags, GST_TAG_MERGE_APPEND, GST_TAG_CODEC, "Apple ProRes",
      NULL);

  *intra_only = TRUE;

  return caps;
}

static const MXFEssenceElementHandler mxf_prores_essence_element_handler = {
  mxf_is_prores_essence_track,
  mxf_prores_get_track_wrapping,
  mxf_prores_create_caps
};

void
mxf_prores_init (void)
{
  mxf_essence_element_handler_register (&mxf_prores_essence_element_handler);
}