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