1 /* ASF parser plugin for GStreamer
2 * Copyright (C) 2009 Thiago Santos <thiagoss@embedded.ufcg.edu.br>
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 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <string.h>
25 #include "gstasfparse.h"
26
27 /* FIXME add this include
28 * #include <gst/gst-i18n-plugin.h> */
29
30 GST_DEBUG_CATEGORY_STATIC (asfparse_debug);
31 #define GST_CAT_DEFAULT asfparse_debug
32
33 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
34 GST_PAD_SRC,
35 GST_PAD_ALWAYS,
36 GST_STATIC_CAPS ("video/x-ms-asf, parsed = (boolean) true")
37 );
38
39 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
40 GST_PAD_SINK,
41 GST_PAD_ALWAYS,
42 GST_STATIC_CAPS ("video/x-ms-asf, parsed = (boolean) false")
43 );
44
45 #define gst_asf_parse_parent_class parent_class
46 G_DEFINE_TYPE (GstAsfParse, gst_asf_parse, GST_TYPE_BASE_PARSE);
47
48 static gboolean
gst_asf_parse_start(GstBaseParse * parse)49 gst_asf_parse_start (GstBaseParse * parse)
50 {
51 GstAsfParse *asfparse = GST_ASF_PARSE_CAST (parse);
52 gst_asf_file_info_reset (asfparse->asfinfo);
53 asfparse->parse_state = ASF_PARSING_HEADERS;
54 asfparse->parsed_packets = 0;
55
56 /* ASF Obj header length */
57 gst_base_parse_set_min_frame_size (GST_BASE_PARSE_CAST (asfparse),
58 ASF_GUID_OBJSIZE_SIZE);
59
60 gst_base_parse_set_syncable (GST_BASE_PARSE_CAST (asfparse), FALSE);
61
62 return TRUE;
63 }
64
65 static gboolean
gst_asf_parse_stop(GstBaseParse * parse)66 gst_asf_parse_stop (GstBaseParse * parse)
67 {
68 GstAsfParse *asfparse = GST_ASF_PARSE_CAST (parse);
69 gst_asf_file_info_reset (asfparse->asfinfo);
70
71 return TRUE;
72 }
73
74 static GstFlowReturn
gst_asf_parse_parse_data_object(GstAsfParse * asfparse,guint8 * data,gsize size)75 gst_asf_parse_parse_data_object (GstAsfParse * asfparse, guint8 * data,
76 gsize size)
77 {
78 GstByteReader reader;
79 GstFlowReturn ret = GST_FLOW_OK;
80 guint64 packet_count = 0;
81
82 GST_DEBUG_OBJECT (asfparse, "Parsing data object");
83
84 gst_byte_reader_init (&reader, data, size);
85 /* skip to packet count */
86 if (!gst_byte_reader_skip (&reader, 40))
87 goto error;
88 if (!gst_byte_reader_get_uint64_le (&reader, &packet_count))
89 goto error;
90
91 if (asfparse->asfinfo->packets_count != packet_count) {
92 GST_WARNING_OBJECT (asfparse, "File properties object and data object have "
93 "different packets count, %" G_GUINT64_FORMAT " %" G_GUINT64_FORMAT,
94 asfparse->asfinfo->packets_count, packet_count);
95 } else {
96 GST_DEBUG_OBJECT (asfparse, "Total packets: %" G_GUINT64_FORMAT,
97 packet_count);
98 }
99
100 return GST_FLOW_OK;
101
102 error:
103 ret = GST_FLOW_ERROR;
104 GST_ERROR_OBJECT (asfparse, "Error while parsing data object headers");
105 return ret;
106 }
107
108 static GstFlowReturn
gst_asf_parse_parse_packet(GstAsfParse * asfparse,GstBaseParseFrame * frame,GstMapInfo * map)109 gst_asf_parse_parse_packet (GstAsfParse * asfparse, GstBaseParseFrame * frame,
110 GstMapInfo * map)
111 {
112 GstBuffer *buffer = frame->buffer;
113 GstAsfPacketInfo *packetinfo = asfparse->packetinfo;
114
115 /* gst_asf_parse_packet_* won't accept size larger than the packet size, so we assume
116 * it will always be packet_size here */
117 g_return_val_if_fail (map->size >= asfparse->asfinfo->packet_size,
118 GST_FLOW_ERROR);
119
120 if (!gst_asf_parse_packet_from_data (map->data,
121 asfparse->asfinfo->packet_size, buffer, packetinfo, FALSE,
122 asfparse->asfinfo->packet_size))
123 goto error;
124
125 GST_DEBUG_OBJECT (asfparse, "Received packet of length %" G_GUINT32_FORMAT
126 ", padding %" G_GUINT32_FORMAT ", send time %" G_GUINT32_FORMAT
127 ", duration %" G_GUINT16_FORMAT " and %s keyframe(s)",
128 packetinfo->packet_size, packetinfo->padding,
129 packetinfo->send_time, packetinfo->duration,
130 (packetinfo->has_keyframe) ? "with" : "without");
131
132 /* set gstbuffer fields */
133 if (!packetinfo->has_keyframe) {
134 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
135 }
136 GST_BUFFER_TIMESTAMP (buffer) = ((GstClockTime) packetinfo->send_time)
137 * GST_MSECOND;
138 GST_BUFFER_DURATION (buffer) = ((GstClockTime) packetinfo->duration)
139 * GST_MSECOND;
140
141 return GST_FLOW_OK;
142
143 error:
144 GST_ERROR_OBJECT (asfparse, "Error while parsing data packet");
145 return GST_FLOW_ERROR;
146 }
147
148
149 /* reads the next object and pushes it through without parsing */
150 static GstFlowReturn
gst_asf_parse_handle_frame_push_object(GstAsfParse * asfparse,GstBaseParseFrame * frame,gint * skipsize,const Guid * guid)151 gst_asf_parse_handle_frame_push_object (GstAsfParse * asfparse,
152 GstBaseParseFrame * frame, gint * skipsize, const Guid * guid)
153 {
154 GstBuffer *buffer = frame->buffer;
155 GstMapInfo map;
156 GstFlowReturn ret = GST_FLOW_OK;
157
158 gst_buffer_map (buffer, &map, GST_MAP_READ);
159 if (map.size >= ASF_GUID_OBJSIZE_SIZE) {
160 guint64 size;
161
162 size = gst_asf_match_and_peek_obj_size (map.data, guid);
163
164 if (size == 0) {
165 GST_ERROR_OBJECT (asfparse, "GUID starting identifier missing");
166 ret = GST_FLOW_ERROR;
167 gst_buffer_unmap (buffer, &map);
168 goto end;
169 }
170
171 if (size > map.size) {
172 /* request all the obj data */
173 gst_base_parse_set_min_frame_size (GST_BASE_PARSE_CAST (asfparse), size);
174 gst_buffer_unmap (buffer, &map);
175 goto end;
176 }
177
178 gst_buffer_unmap (buffer, &map);
179
180 gst_base_parse_set_min_frame_size (GST_BASE_PARSE_CAST (asfparse),
181 ASF_GUID_OBJSIZE_SIZE);
182 gst_base_parse_finish_frame (GST_BASE_PARSE_CAST (asfparse), frame, size);
183 } else {
184 gst_buffer_unmap (buffer, &map);
185 *skipsize = 0;
186 }
187
188 end:
189 return ret;
190 }
191
192 static GstFlowReturn
gst_asf_parse_handle_frame_headers(GstAsfParse * asfparse,GstBaseParseFrame * frame,gint * skipsize)193 gst_asf_parse_handle_frame_headers (GstAsfParse * asfparse,
194 GstBaseParseFrame * frame, gint * skipsize)
195 {
196 GstBuffer *buffer = frame->buffer;
197 GstMapInfo map;
198 GstFlowReturn ret = GST_FLOW_OK;
199
200 gst_buffer_map (buffer, &map, GST_MAP_READ);
201 if (map.size >= ASF_GUID_OBJSIZE_SIZE) {
202 guint64 size;
203
204 size = gst_asf_match_and_peek_obj_size (map.data,
205 &(guids[ASF_HEADER_OBJECT_INDEX]));
206
207 if (size == 0) {
208 GST_ERROR_OBJECT (asfparse, "ASF starting identifier missing");
209 ret = GST_FLOW_ERROR;
210 gst_buffer_unmap (buffer, &map);
211 goto end;
212 }
213
214 if (size > map.size) {
215 /* request all the obj data */
216 gst_base_parse_set_min_frame_size (GST_BASE_PARSE_CAST (asfparse), size);
217 gst_buffer_unmap (buffer, &map);
218 goto end;
219 }
220
221 if (gst_asf_parse_headers_from_data (map.data, map.size, asfparse->asfinfo)) {
222 GST_DEBUG_OBJECT (asfparse, "Successfully parsed headers");
223 asfparse->parse_state = ASF_PARSING_DATA;
224 gst_buffer_unmap (buffer, &map);
225
226 GST_INFO_OBJECT (asfparse, "Broadcast mode %s",
227 asfparse->asfinfo->broadcast ? "on" : "off");
228
229 gst_base_parse_set_min_frame_size (GST_BASE_PARSE_CAST (asfparse),
230 ASF_GUID_OBJSIZE_SIZE);
231
232 gst_pad_push_event (GST_BASE_PARSE_SRC_PAD (asfparse),
233 gst_event_new_caps (gst_caps_new_simple ("video/x-ms-asf", "parsed",
234 G_TYPE_BOOLEAN, TRUE, NULL)));
235 gst_base_parse_finish_frame (GST_BASE_PARSE_CAST (asfparse), frame, size);
236 } else {
237 ret = GST_FLOW_ERROR;
238 }
239 } else {
240 gst_buffer_unmap (buffer, &map);
241 *skipsize = 0;
242 }
243
244 end:
245 return ret;
246 }
247
248 static GstFlowReturn
gst_asf_parse_handle_frame_data_header(GstAsfParse * asfparse,GstBaseParseFrame * frame,gint * skipsize)249 gst_asf_parse_handle_frame_data_header (GstAsfParse * asfparse,
250 GstBaseParseFrame * frame, gint * skipsize)
251 {
252 GstBuffer *buffer = frame->buffer;
253 GstMapInfo map;
254 GstFlowReturn ret = GST_FLOW_OK;
255
256 gst_buffer_map (buffer, &map, GST_MAP_READ);
257 if (map.size >= ASF_GUID_OBJSIZE_SIZE) {
258 guint64 size;
259
260 size = gst_asf_match_and_peek_obj_size (map.data,
261 &(guids[ASF_DATA_OBJECT_INDEX]));
262
263 if (size == 0) {
264 GST_ERROR_OBJECT (asfparse, "ASF data object missing");
265 ret = GST_FLOW_ERROR;
266 gst_buffer_unmap (buffer, &map);
267 goto end;
268 }
269
270 if (ASF_DATA_OBJECT_SIZE > map.size) {
271 /* request all the obj data header size */
272 gst_base_parse_set_min_frame_size (GST_BASE_PARSE_CAST (asfparse),
273 ASF_DATA_OBJECT_SIZE);
274 gst_buffer_unmap (buffer, &map);
275 goto end;
276 }
277
278 if (gst_asf_parse_parse_data_object (asfparse, map.data,
279 map.size) == GST_FLOW_OK) {
280 GST_DEBUG_OBJECT (asfparse, "Successfully parsed data object");
281 asfparse->parse_state = ASF_PARSING_PACKETS;
282 gst_buffer_unmap (buffer, &map);
283
284 gst_base_parse_set_min_frame_size (GST_BASE_PARSE_CAST (asfparse),
285 asfparse->asfinfo->packet_size);
286
287 gst_base_parse_finish_frame (GST_BASE_PARSE_CAST (asfparse), frame,
288 ASF_DATA_OBJECT_SIZE);
289 }
290 } else {
291 gst_buffer_unmap (buffer, &map);
292 *skipsize = 0;
293 }
294
295 end:
296 return ret;
297 }
298
299 static GstFlowReturn
gst_asf_parse_handle_frame_packets(GstAsfParse * asfparse,GstBaseParseFrame * frame,gint * skipsize)300 gst_asf_parse_handle_frame_packets (GstAsfParse * asfparse,
301 GstBaseParseFrame * frame, gint * skipsize)
302 {
303 GstBuffer *buffer = frame->buffer;
304 GstMapInfo map;
305 GstFlowReturn ret = GST_FLOW_OK;
306
307 GST_LOG_OBJECT (asfparse, "Packet parsing");
308 gst_buffer_map (buffer, &map, GST_MAP_READ);
309 if (G_LIKELY (map.size >= asfparse->asfinfo->packet_size)) {
310
311 GST_DEBUG_OBJECT (asfparse, "Parsing packet %" G_GUINT64_FORMAT,
312 asfparse->parsed_packets);
313
314 ret = gst_asf_parse_parse_packet (asfparse, frame, &map);
315
316 gst_buffer_unmap (buffer, &map);
317
318 if (ret == GST_FLOW_OK) {
319 asfparse->parsed_packets++;
320 gst_base_parse_finish_frame (GST_BASE_PARSE_CAST (asfparse), frame,
321 asfparse->asfinfo->packet_size);
322
323 /* test if all packets have been processed */
324 if (G_UNLIKELY (!asfparse->asfinfo->broadcast &&
325 asfparse->parsed_packets == asfparse->asfinfo->packets_count)) {
326 GST_INFO_OBJECT (asfparse,
327 "All %" G_GUINT64_FORMAT " packets processed",
328 asfparse->parsed_packets);
329 asfparse->parse_state = ASF_PARSING_INDEXES;
330 gst_base_parse_set_min_frame_size (GST_BASE_PARSE_CAST (asfparse),
331 ASF_GUID_OBJSIZE_SIZE);
332 }
333 }
334 } else {
335 gst_base_parse_set_min_frame_size (GST_BASE_PARSE_CAST (asfparse),
336 asfparse->asfinfo->packet_size);
337 gst_buffer_unmap (buffer, &map);
338 *skipsize = 0;
339 }
340
341 return ret;
342 }
343
344 static GstFlowReturn
gst_asf_parse_handle_frame_indexes(GstAsfParse * asfparse,GstBaseParseFrame * frame,gint * skipsize)345 gst_asf_parse_handle_frame_indexes (GstAsfParse * asfparse,
346 GstBaseParseFrame * frame, gint * skipsize)
347 {
348 /* don't care about indexes, just push them forward */
349 return gst_asf_parse_handle_frame_push_object (asfparse, frame, skipsize,
350 NULL);
351 }
352
353
354 static GstFlowReturn
gst_asf_parse_handle_frame(GstBaseParse * parse,GstBaseParseFrame * frame,gint * skipsize)355 gst_asf_parse_handle_frame (GstBaseParse * parse,
356 GstBaseParseFrame * frame, gint * skipsize)
357 {
358 GstAsfParse *asfparse = GST_ASF_PARSE_CAST (parse);
359
360 switch (asfparse->parse_state) {
361 case ASF_PARSING_HEADERS:
362 return gst_asf_parse_handle_frame_headers (asfparse, frame, skipsize);
363 case ASF_PARSING_DATA:
364 return gst_asf_parse_handle_frame_data_header (asfparse, frame, skipsize);
365 case ASF_PARSING_PACKETS:
366 return gst_asf_parse_handle_frame_packets (asfparse, frame, skipsize);
367 case ASF_PARSING_INDEXES:
368 return gst_asf_parse_handle_frame_indexes (asfparse, frame, skipsize);
369 default:
370 break;
371 }
372
373 g_assert_not_reached ();
374 return GST_FLOW_ERROR;
375 }
376
377 static void
gst_asf_parse_finalize(GObject * object)378 gst_asf_parse_finalize (GObject * object)
379 {
380 GstAsfParse *asfparse = GST_ASF_PARSE (object);
381 gst_asf_file_info_free (asfparse->asfinfo);
382 g_free (asfparse->packetinfo);
383 G_OBJECT_CLASS (parent_class)->finalize (object);
384 }
385
386 static void
gst_asf_parse_class_init(GstAsfParseClass * klass)387 gst_asf_parse_class_init (GstAsfParseClass * klass)
388 {
389 GObjectClass *gobject_class;
390 GstElementClass *gstelement_class;
391 GstBaseParseClass *gstbaseparse_class;
392
393 gobject_class = (GObjectClass *) klass;
394 gstelement_class = (GstElementClass *) klass;
395 gstbaseparse_class = (GstBaseParseClass *) klass;
396
397 gobject_class->finalize = gst_asf_parse_finalize;
398
399 gstbaseparse_class->start = gst_asf_parse_start;
400 gstbaseparse_class->stop = gst_asf_parse_stop;
401 gstbaseparse_class->handle_frame = gst_asf_parse_handle_frame;
402
403 gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
404 gst_element_class_add_static_pad_template (gstelement_class, &sink_factory);
405
406 gst_element_class_set_static_metadata (gstelement_class, "ASF parser",
407 "Parser", "Parses ASF", "Thiago Santos <thiagoss@embedded.ufcg.edu.br>");
408
409 GST_DEBUG_CATEGORY_INIT (asfparse_debug, "asfparse", 0,
410 "Parser for ASF streams");
411 }
412
413 static void
gst_asf_parse_init(GstAsfParse * asfparse)414 gst_asf_parse_init (GstAsfParse * asfparse)
415 {
416 asfparse->asfinfo = gst_asf_file_info_new ();
417 asfparse->packetinfo = g_new0 (GstAsfPacketInfo, 1);
418 }
419
420 gboolean
gst_asf_parse_plugin_init(GstPlugin * plugin)421 gst_asf_parse_plugin_init (GstPlugin * plugin)
422 {
423 return gst_element_register (plugin, "asfparse",
424 GST_RANK_NONE, GST_TYPE_ASF_PARSE);
425 }
426