GstMoofBox * gst_isoff_moof_box_parse (GstByteReader * reader) { GstMoofBox *moof; gboolean had_mfhd = FALSE; moof = g_new0 (GstMoofBox, 1); moof->traf = g_array_new (FALSE, FALSE, sizeof (GstTrafBox)); g_array_set_clear_func (moof->traf, (GDestroyNotify) gst_isoff_traf_box_clear); while (gst_byte_reader_get_remaining (reader) > 0) { guint32 fourcc; guint header_size; guint64 size; if (!gst_isoff_parse_box_header (reader, &fourcc, NULL, &header_size, &size)) goto error; if (gst_byte_reader_get_remaining (reader) < size - header_size) goto error; switch (fourcc) { case GST_ISOFF_FOURCC_MFHD:{ GstByteReader sub_reader; gst_byte_reader_get_sub_reader (reader, &sub_reader, size - header_size); if (!gst_isoff_mfhd_box_parse (&moof->mfhd, &sub_reader)) goto error; had_mfhd = TRUE; break; } case GST_ISOFF_FOURCC_TRAF:{ GstByteReader sub_reader; GstTrafBox traf; gst_byte_reader_get_sub_reader (reader, &sub_reader, size - header_size); if (!gst_isoff_traf_box_parse (&traf, &sub_reader)) goto error; g_array_append_val (moof->traf, traf); break; } default: gst_byte_reader_skip (reader, size - header_size); break; } } if (!had_mfhd) goto error; return moof; error: gst_isoff_moof_box_free (moof); return NULL; }
static gboolean gst_isoff_traf_box_parse (GstTrafBox * traf, GstByteReader * reader) { gboolean had_tfhd = FALSE; memset (traf, 0, sizeof (*traf)); traf->trun = g_array_new (FALSE, FALSE, sizeof (GstTrunBox)); g_array_set_clear_func (traf->trun, (GDestroyNotify) gst_isoff_trun_box_clear); while (gst_byte_reader_get_remaining (reader) > 0) { guint32 fourcc; guint header_size; guint64 size; if (!gst_isoff_parse_box_header (reader, &fourcc, NULL, &header_size, &size)) goto error; if (gst_byte_reader_get_remaining (reader) < size - header_size) goto error; switch (fourcc) { case GST_ISOFF_FOURCC_TFHD:{ GstByteReader sub_reader; gst_byte_reader_get_sub_reader (reader, &sub_reader, size - header_size); if (!gst_isoff_tfhd_box_parse (&traf->tfhd, &sub_reader)) goto error; had_tfhd = TRUE; break; } case GST_ISOFF_FOURCC_TRUN:{ GstByteReader sub_reader; GstTrunBox trun; gst_byte_reader_get_sub_reader (reader, &sub_reader, size - header_size); if (!gst_isoff_trun_box_parse (&trun, &sub_reader)) goto error; g_array_append_val (traf->trun, trun); break; } default: gst_byte_reader_skip (reader, size - header_size); break; } } if (!had_tfhd) goto error; return TRUE; error: gst_isoff_traf_box_clear (traf); return FALSE; }
static gboolean flx_decode_chunks (GstFlxDec * flxdec, gulong n_chunks, GstByteReader * reader, GstByteWriter * writer) { gboolean ret = TRUE; while (n_chunks--) { GstByteReader chunk; guint32 size; guint16 type; if (!gst_byte_reader_get_uint32_le (reader, &size)) goto parse_error; if (!gst_byte_reader_get_uint16_le (reader, &type)) goto parse_error; GST_LOG_OBJECT (flxdec, "chunk has type 0x%02x size %d", type, size); if (!gst_byte_reader_get_sub_reader (reader, &chunk, size - FlxFrameChunkSize)) { GST_ERROR_OBJECT (flxdec, "Incorrect size in the chunk header"); goto error; } switch (type) { case FLX_COLOR64: ret = flx_decode_color (flxdec, &chunk, writer, 2); break; case FLX_COLOR256: ret = flx_decode_color (flxdec, &chunk, writer, 0); break; case FLX_BRUN: ret = flx_decode_brun (flxdec, &chunk, writer); break; case FLX_LC: ret = flx_decode_delta_fli (flxdec, &chunk, writer); break; case FLX_SS2: ret = flx_decode_delta_flc (flxdec, &chunk, writer); break; case FLX_BLACK: ret = gst_byte_writer_fill (writer, 0, flxdec->size); break; case FLX_MINI: break; default: GST_WARNING ("Unimplemented chunk type: 0x%02x size: %d - skipping", type, size); break; } if (!ret) break; } return ret; parse_error: GST_ERROR_OBJECT (flxdec, "Failed to decode chunk"); error: return FALSE; }
static GstFlowReturn gst_flxdec_chain (GstPad * pad, GstObject * parent, GstBuffer * buf) { GstByteReader reader; GstBuffer *input; GstMapInfo map_info; GstCaps *caps; guint available; GstFlowReturn res = GST_FLOW_OK; GstFlxDec *flxdec; FlxHeader *flxh; g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR); flxdec = (GstFlxDec *) parent; g_return_val_if_fail (flxdec != NULL, GST_FLOW_ERROR); gst_adapter_push (flxdec->adapter, buf); available = gst_adapter_available (flxdec->adapter); input = gst_adapter_get_buffer (flxdec->adapter, available); if (!gst_buffer_map (input, &map_info, GST_MAP_READ)) { GST_ELEMENT_ERROR (flxdec, STREAM, DECODE, ("%s", "Failed to map buffer"), (NULL)); goto error; } gst_byte_reader_init (&reader, map_info.data, map_info.size); if (flxdec->state == GST_FLXDEC_READ_HEADER) { if (available >= FlxHeaderSize) { GstByteReader header; GstCaps *templ; if (!gst_byte_reader_get_sub_reader (&reader, &header, FlxHeaderSize)) { GST_ELEMENT_ERROR (flxdec, STREAM, DECODE, ("%s", "Could not read header"), (NULL)); goto unmap_input_error; } gst_adapter_flush (flxdec->adapter, FlxHeaderSize); available -= FlxHeaderSize; if (!_read_flx_header (flxdec, &header, &flxdec->hdr)) { GST_ELEMENT_ERROR (flxdec, STREAM, DECODE, ("%s", "Failed to parse header"), (NULL)); goto unmap_input_error; } flxh = &flxdec->hdr; /* check header */ if (flxh->type != FLX_MAGICHDR_FLI && flxh->type != FLX_MAGICHDR_FLC && flxh->type != FLX_MAGICHDR_FLX) { GST_ELEMENT_ERROR (flxdec, STREAM, WRONG_TYPE, (NULL), ("not a flx file (type %x)", flxh->type)); goto unmap_input_error; } GST_INFO_OBJECT (flxdec, "size : %d", flxh->size); GST_INFO_OBJECT (flxdec, "frames : %d", flxh->frames); GST_INFO_OBJECT (flxdec, "width : %d", flxh->width); GST_INFO_OBJECT (flxdec, "height : %d", flxh->height); GST_INFO_OBJECT (flxdec, "depth : %d", flxh->depth); GST_INFO_OBJECT (flxdec, "speed : %d", flxh->speed); flxdec->next_time = 0; if (flxh->type == FLX_MAGICHDR_FLI) { flxdec->frame_time = JIFFIE * flxh->speed; } else if (flxh->speed == 0) { flxdec->frame_time = GST_SECOND / 70; } else { flxdec->frame_time = flxh->speed * GST_MSECOND; } flxdec->duration = flxh->frames * flxdec->frame_time; GST_LOG ("duration : %" GST_TIME_FORMAT, GST_TIME_ARGS (flxdec->duration)); templ = gst_pad_get_pad_template_caps (flxdec->srcpad); caps = gst_caps_copy (templ); gst_caps_unref (templ); gst_caps_set_simple (caps, "width", G_TYPE_INT, flxh->width, "height", G_TYPE_INT, flxh->height, "framerate", GST_TYPE_FRACTION, (gint) GST_MSECOND, (gint) flxdec->frame_time / 1000, NULL); gst_pad_set_caps (flxdec->srcpad, caps); gst_caps_unref (caps); if (flxdec->need_segment) { gst_pad_push_event (flxdec->srcpad, gst_event_new_segment (&flxdec->segment)); flxdec->need_segment = FALSE; } /* zero means 8 */ if (flxh->depth == 0) flxh->depth = 8; if (flxh->depth != 8) { GST_ELEMENT_ERROR (flxdec, STREAM, WRONG_TYPE, ("%s", "Don't know how to decode non 8 bit depth streams"), (NULL)); goto unmap_input_error; } flxdec->converter = flx_colorspace_converter_new (flxh->width, flxh->height); if (flxh->type == FLX_MAGICHDR_FLC || flxh->type == FLX_MAGICHDR_FLX) { GST_INFO_OBJECT (flxdec, "(FLC) aspect_dx : %d", flxh->aspect_dx); GST_INFO_OBJECT (flxdec, "(FLC) aspect_dy : %d", flxh->aspect_dy); GST_INFO_OBJECT (flxdec, "(FLC) oframe1 : 0x%08x", flxh->oframe1); GST_INFO_OBJECT (flxdec, "(FLC) oframe2 : 0x%08x", flxh->oframe2); } flxdec->size = ((guint) flxh->width * (guint) flxh->height); if (flxdec->size >= G_MAXSIZE / 4) { GST_ELEMENT_ERROR (flxdec, STREAM, DECODE, ("%s", "Cannot allocate required memory"), (NULL)); goto unmap_input_error; } /* create delta and output frame */ flxdec->frame_data = g_malloc0 (flxdec->size); flxdec->delta_data = g_malloc0 (flxdec->size); flxdec->state = GST_FLXDEC_PLAYING; } } else if (flxdec->state == GST_FLXDEC_PLAYING) { GstBuffer *out; /* while we have enough data in the adapter */ while (available >= FlxFrameChunkSize && res == GST_FLOW_OK) { guint32 size; guint16 type; if (!gst_byte_reader_get_uint32_le (&reader, &size)) goto parse_error; if (available < size) goto need_more_data; available -= size; gst_adapter_flush (flxdec->adapter, size); if (!gst_byte_reader_get_uint16_le (&reader, &type)) goto parse_error; switch (type) { case FLX_FRAME_TYPE:{ GstByteReader chunks; GstByteWriter writer; guint16 n_chunks; GstMapInfo map; GST_LOG_OBJECT (flxdec, "Have frame type 0x%02x of size %d", type, size); if (!gst_byte_reader_get_sub_reader (&reader, &chunks, size - FlxFrameChunkSize)) goto parse_error; if (!gst_byte_reader_get_uint16_le (&chunks, &n_chunks)) goto parse_error; GST_LOG_OBJECT (flxdec, "Have %d chunks", n_chunks); if (n_chunks == 0) break; if (!gst_byte_reader_skip (&chunks, 8)) /* reserved */ goto parse_error; gst_byte_writer_init_with_data (&writer, flxdec->frame_data, flxdec->size, TRUE); /* decode chunks */ if (!flx_decode_chunks (flxdec, n_chunks, &chunks, &writer)) { GST_ELEMENT_ERROR (flxdec, STREAM, DECODE, ("%s", "Could not decode chunk"), NULL); goto unmap_input_error; } gst_byte_writer_reset (&writer); /* save copy of the current frame for possible delta. */ memcpy (flxdec->delta_data, flxdec->frame_data, flxdec->size); out = gst_buffer_new_and_alloc (flxdec->size * 4); if (!gst_buffer_map (out, &map, GST_MAP_WRITE)) { GST_ELEMENT_ERROR (flxdec, STREAM, DECODE, ("%s", "Could not map output buffer"), NULL); gst_buffer_unref (out); goto unmap_input_error; } /* convert current frame. */ flx_colorspace_convert (flxdec->converter, flxdec->frame_data, map.data); gst_buffer_unmap (out, &map); GST_BUFFER_TIMESTAMP (out) = flxdec->next_time; flxdec->next_time += flxdec->frame_time; res = gst_pad_push (flxdec->srcpad, out); break; } default: GST_DEBUG_OBJECT (flxdec, "Unknown frame type 0x%02x, skipping %d", type, size); if (!gst_byte_reader_skip (&reader, size - FlxFrameChunkSize)) goto parse_error; break; } } } need_more_data: gst_buffer_unmap (input, &map_info); gst_buffer_unref (input); return res; /* ERRORS */ parse_error: GST_ELEMENT_ERROR (flxdec, STREAM, DECODE, ("%s", "Failed to parse stream"), (NULL)); unmap_input_error: gst_buffer_unmap (input, &map_info); error: gst_buffer_unref (input); return GST_FLOW_ERROR; }
gboolean gst_mss_fragment_parser_add_buffer (GstMssFragmentParser * parser, GstBuffer * buffer) { GstByteReader reader; GstMapInfo info; guint64 size; guint32 fourcc; guint header_size; gboolean error = FALSE; if (!gst_buffer_map (buffer, &info, GST_MAP_READ)) { return FALSE; } gst_byte_reader_init (&reader, info.data, info.size); GST_TRACE ("Total buffer size: %u", gst_byte_reader_get_size (&reader)); do { parser->current_fourcc = 0; if (!gst_isoff_parse_box_header (&reader, &fourcc, NULL, &header_size, &size)) { break; } parser->current_fourcc = fourcc; GST_LOG ("box %" GST_FOURCC_FORMAT " size %" G_GUINT64_FORMAT, GST_FOURCC_ARGS (fourcc), size); parser->current_fourcc = fourcc; if (parser->current_fourcc == GST_ISOFF_FOURCC_MOOF) { GstByteReader sub_reader; g_assert (parser->moof == NULL); gst_byte_reader_get_sub_reader (&reader, &sub_reader, size - header_size); parser->moof = gst_isoff_moof_box_parse (&sub_reader); if (parser->moof == NULL) { GST_ERROR ("Failed to parse moof"); error = TRUE; } } else if (parser->current_fourcc == GST_ISOFF_FOURCC_MDAT) { goto beach; } else { gst_byte_reader_skip (&reader, size - header_size); } } while (gst_byte_reader_get_remaining (&reader) > 0); beach: /* Do sanity check */ if (parser->current_fourcc != GST_ISOFF_FOURCC_MDAT || !parser->moof || parser->moof->traf->len == 0) error = TRUE; if (!error) { GstTrafBox *traf = &g_array_index (parser->moof->traf, GstTrafBox, 0); if (!traf->tfxd) { GST_ERROR ("no tfxd box"); error = TRUE; } else if (!traf->tfrf) { GST_ERROR ("no tfrf box"); error = TRUE; } } if (!error) parser->status = GST_MSS_FRAGMENT_HEADER_PARSER_FINISHED; GST_LOG ("Fragment parsing successful: %s", error ? "no" : "yes"); gst_buffer_unmap (buffer, &info); return !error; }