mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer.git
synced 2024-11-22 01:31:03 +00:00
The new riff parser and encoder
Original commit message from CVS: The new riff parser and encoder
This commit is contained in:
parent
0d9315223a
commit
82cdfe8ba3
3 changed files with 434 additions and 0 deletions
178
libs/riff/gstriffencode.c
Normal file
178
libs/riff/gstriffencode.c
Normal file
|
@ -0,0 +1,178 @@
|
|||
/* Gnome-Streamer
|
||||
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
|
||||
*
|
||||
* 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., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
|
||||
#include <gstriff.h>
|
||||
|
||||
#define GST_RIFF_ENCODER_BUF_SIZE 1024
|
||||
|
||||
//#define debug(format,args...) g_print(format,##args)
|
||||
#define debug(format,args...)
|
||||
|
||||
#define ADD_CHUNK(riffenc, chunkid, chunksize) \
|
||||
{ \
|
||||
gst_riff_chunk *chunk;\
|
||||
chunk = (gst_riff_chunk *)(riffenc->dataleft + riffenc->nextlikely);\
|
||||
chunk->id = chunkid; \
|
||||
chunk->size = chunksize; \
|
||||
riffenc->nextlikely += sizeof(gst_riff_chunk); \
|
||||
}
|
||||
|
||||
#define ADD_LIST(riffenc, listsize, listtype) \
|
||||
{ \
|
||||
gst_riff_list *list;\
|
||||
list = (gst_riff_list *)(riffenc->dataleft + riffenc->nextlikely); \
|
||||
list->id = GST_RIFF_TAG_LIST; \
|
||||
list->size = listsize; \
|
||||
list->type = listtype; \
|
||||
riffenc->nextlikely += sizeof(gst_riff_list); \
|
||||
}
|
||||
|
||||
|
||||
GstRiff *gst_riff_encoder_new(guint32 type) {
|
||||
GstRiff *riff;
|
||||
gst_riff_list *list;
|
||||
|
||||
debug("gst_riff_encoder: making %4.4s encoder\n", (char *)&type);
|
||||
riff = (GstRiff *)g_malloc(sizeof(GstRiff));
|
||||
g_return_val_if_fail(riff != NULL, NULL);
|
||||
|
||||
riff->form = 0;
|
||||
riff->chunks = NULL;
|
||||
riff->state = GST_RIFF_STATE_INITIAL;
|
||||
riff->curoffset = 0;
|
||||
riff->incomplete_chunk = NULL;
|
||||
riff->dataleft = g_malloc(GST_RIFF_ENCODER_BUF_SIZE);
|
||||
riff->dataleft_size = GST_RIFF_ENCODER_BUF_SIZE;
|
||||
riff->nextlikely = 0;
|
||||
|
||||
list = (gst_riff_list *)riff->dataleft;
|
||||
list->id = GST_RIFF_TAG_RIFF;
|
||||
list->size = 0x00FFFFFF;
|
||||
list->type = GST_RIFF_RIFF_AVI;
|
||||
|
||||
riff->nextlikely += sizeof(gst_riff_list);
|
||||
|
||||
return riff;
|
||||
}
|
||||
|
||||
gint gst_riff_encoder_avih(GstRiff *riff, gst_riff_avih *head, gulong size) {
|
||||
gst_riff_chunk *chunk;
|
||||
|
||||
g_return_val_if_fail(riff->state == GST_RIFF_STATE_INITIAL, GST_RIFF_EINVAL);
|
||||
|
||||
debug("gst_riff_encoder: add avih\n");
|
||||
|
||||
ADD_LIST(riff, 0xB8, GST_RIFF_LIST_hdrl);
|
||||
|
||||
ADD_CHUNK(riff, GST_RIFF_TAG_avih, size);
|
||||
|
||||
chunk = (gst_riff_chunk *)(riff->dataleft + riff->nextlikely);
|
||||
memcpy(chunk, head, size);
|
||||
riff->nextlikely += size;
|
||||
|
||||
riff->state = GST_RIFF_STATE_HASAVIH;
|
||||
return GST_RIFF_OK;
|
||||
}
|
||||
|
||||
gint gst_riff_encoder_strh(GstRiff *riff, guint32 fcc_type, gst_riff_strh *head, gulong size) {
|
||||
gst_riff_chunk *chunk;
|
||||
|
||||
g_return_val_if_fail(riff->state == GST_RIFF_STATE_HASAVIH ||
|
||||
riff->state == GST_RIFF_STATE_HASSTRF, GST_RIFF_EINVAL);
|
||||
|
||||
debug("gst_riff_encoder: add strh type %08x (%4.4s)\n", fcc_type, (char *)&fcc_type);
|
||||
|
||||
ADD_LIST(riff, 108, GST_RIFF_LIST_strl);
|
||||
|
||||
ADD_CHUNK(riff, GST_RIFF_TAG_strh, size);
|
||||
|
||||
chunk = (gst_riff_chunk *)(riff->dataleft + riff->nextlikely);
|
||||
head->type = fcc_type;
|
||||
memcpy(chunk, head, size);
|
||||
|
||||
riff->nextlikely += size;
|
||||
|
||||
riff->state = GST_RIFF_STATE_HASSTRH;
|
||||
return GST_RIFF_OK;
|
||||
}
|
||||
|
||||
gint gst_riff_encoder_strf(GstRiff *riff, void *format, gulong size) {
|
||||
gst_riff_chunk *chunk;
|
||||
|
||||
g_return_val_if_fail(riff->state == GST_RIFF_STATE_HASSTRH, GST_RIFF_EINVAL);
|
||||
|
||||
debug("gst_riff_encoder: add strf\n");
|
||||
|
||||
ADD_CHUNK(riff, GST_RIFF_TAG_strf, size);
|
||||
|
||||
chunk = (gst_riff_chunk *)(riff->dataleft + riff->nextlikely);
|
||||
memcpy(chunk, format, size);
|
||||
riff->nextlikely += size;
|
||||
|
||||
riff->state = GST_RIFF_STATE_HASSTRF;
|
||||
return GST_RIFF_OK;
|
||||
}
|
||||
|
||||
gint gst_riff_encoder_chunk(GstRiff *riff, guint32 chunk_type, void *chunkdata, gulong size) {
|
||||
gst_riff_chunk *chunk;
|
||||
|
||||
g_return_val_if_fail(riff->state == GST_RIFF_STATE_HASSTRF ||
|
||||
riff->state == GST_RIFF_STATE_MOVI, GST_RIFF_EINVAL);
|
||||
|
||||
if (riff->state != GST_RIFF_STATE_MOVI) {
|
||||
ADD_LIST(riff, 0x00FFFFFF, GST_RIFF_LIST_movi);
|
||||
riff->state = GST_RIFF_STATE_MOVI;
|
||||
}
|
||||
|
||||
debug("gst_riff_encoder: add chunk type %08x (%4.4s)\n", chunk_type, (char *)&chunk_type);
|
||||
|
||||
ADD_CHUNK(riff, chunk_type, size);
|
||||
|
||||
if (chunkdata != NULL) {
|
||||
chunk = (gst_riff_chunk *)(riff->dataleft + riff->nextlikely);
|
||||
memcpy(chunk, chunkdata, size);
|
||||
riff->nextlikely += size;
|
||||
}
|
||||
|
||||
return GST_RIFF_OK;
|
||||
}
|
||||
|
||||
GstBuffer *gst_riff_encoder_get_buffer(GstRiff *riff) {
|
||||
GstBuffer *newbuf;
|
||||
|
||||
newbuf = gst_buffer_new();
|
||||
GST_BUFFER_DATA(newbuf) = riff->dataleft;
|
||||
GST_BUFFER_SIZE(newbuf) = riff->nextlikely;
|
||||
|
||||
return newbuf;
|
||||
}
|
||||
|
||||
GstBuffer *gst_riff_encoder_get_and_reset_buffer(GstRiff *riff) {
|
||||
GstBuffer *newbuf;
|
||||
|
||||
newbuf = gst_riff_encoder_get_buffer(riff);
|
||||
|
||||
riff->dataleft = g_malloc(GST_RIFF_ENCODER_BUF_SIZE);
|
||||
riff->dataleft_size = GST_RIFF_ENCODER_BUF_SIZE;
|
||||
riff->nextlikely = 0;
|
||||
|
||||
return newbuf;
|
||||
}
|
||||
|
210
libs/riff/gstriffparse.c
Normal file
210
libs/riff/gstriffparse.c
Normal file
|
@ -0,0 +1,210 @@
|
|||
/* Gnome-Streamer
|
||||
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
|
||||
*
|
||||
* 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., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
|
||||
#include <gstriff.h>
|
||||
|
||||
//#define debug(format,args...) g_print(format,##args)
|
||||
#define debug(format,args...)
|
||||
|
||||
|
||||
GstRiff *gst_riff_parser_new(GstRiffCallback function, gpointer data) {
|
||||
GstRiff *riff;
|
||||
|
||||
riff = (GstRiff *)g_malloc(sizeof(GstRiff));
|
||||
g_return_val_if_fail(riff != NULL, NULL);
|
||||
|
||||
riff->form = 0;
|
||||
riff->chunks = NULL;
|
||||
riff->state = 0;
|
||||
riff->curoffset = 0;
|
||||
riff->nextlikely = 0;
|
||||
riff->new_tag_found = function;
|
||||
riff->callback_data = data;
|
||||
riff->incomplete_chunk = NULL;
|
||||
riff->dataleft = NULL;
|
||||
|
||||
return riff;
|
||||
}
|
||||
|
||||
gint gst_riff_parser_next_buffer(GstRiff *riff,GstBuffer *buf,gulong off) {
|
||||
gulong last, size;
|
||||
GstRiffChunk *chunk;
|
||||
|
||||
g_return_val_if_fail(riff != NULL, GST_RIFF_EINVAL);
|
||||
g_return_val_if_fail(buf != NULL, GST_RIFF_EINVAL);
|
||||
g_return_val_if_fail(GST_BUFFER_DATA(buf) != NULL, GST_RIFF_EINVAL);
|
||||
|
||||
size = GST_BUFFER_SIZE(buf);
|
||||
last = off + size;
|
||||
|
||||
debug("gst_riff_parser: offset new buffer 0x%08lx size 0x%08x\n", off, GST_BUFFER_SIZE(buf));
|
||||
|
||||
if (riff->dataleft) {
|
||||
gulong newsize;
|
||||
|
||||
debug("gst_riff_parser: recovering left data\n");
|
||||
newsize = riff->dataleft_size + size;
|
||||
riff->dataleft = g_realloc(riff->dataleft, newsize);
|
||||
memcpy(riff->dataleft+riff->dataleft_size, GST_BUFFER_DATA(buf), size);
|
||||
gst_buffer_unref(buf);
|
||||
|
||||
buf = gst_buffer_new();
|
||||
GST_BUFFER_DATA(buf) = riff->dataleft;
|
||||
GST_BUFFER_SIZE(buf) = newsize;
|
||||
off -= riff->dataleft_size;
|
||||
//last -= riff->dataleft_size;
|
||||
riff->dataleft = NULL;
|
||||
}
|
||||
|
||||
if (off == 0) {
|
||||
gulong *words = (gulong *)GST_BUFFER_DATA(buf);
|
||||
|
||||
// don't even try to parse the head if it's not there FIXME
|
||||
if (last < 12) {
|
||||
riff->state = GST_RIFF_ENOTRIFF;
|
||||
return riff->state;
|
||||
}
|
||||
|
||||
//g_print("testing is 0x%08lx '%s'\n",words[0],gst_riff_id_to_fourcc(words[0]));
|
||||
/* verify this is a valid RIFF file, first of all */
|
||||
if (words[0] != GST_RIFF_TAG_RIFF) {
|
||||
riff->state = GST_RIFF_ENOTRIFF;
|
||||
return riff->state;
|
||||
}
|
||||
riff->form = words[2];
|
||||
//g_print("form is 0x%08lx '%s'\n",words[2],gst_riff_id_to_fourcc(words[2]));
|
||||
riff->nextlikely = 12; /* skip 'RIFF', length, and form */
|
||||
// all OK here
|
||||
riff->incomplete_chunk = NULL;
|
||||
}
|
||||
|
||||
// if we have an incomplete chunk from the previous buffer
|
||||
if (riff->incomplete_chunk) {
|
||||
guint leftover;
|
||||
debug("gst_riff_parser: have incomplete chunk %08x filled\n", riff->incomplete_chunk_size);
|
||||
leftover = riff->incomplete_chunk->size - riff->incomplete_chunk_size;
|
||||
if (leftover <= size) {
|
||||
debug("gst_riff_parser: we can fill it from %08x with %08x bytes = %08x\n",
|
||||
riff->incomplete_chunk_size, leftover,
|
||||
riff->incomplete_chunk_size+leftover);
|
||||
memcpy(riff->incomplete_chunk->data+riff->incomplete_chunk_size, GST_BUFFER_DATA(buf), leftover);
|
||||
|
||||
if (riff->new_tag_found) {
|
||||
riff->new_tag_found(riff->incomplete_chunk, riff->callback_data);
|
||||
}
|
||||
g_free(riff->incomplete_chunk->data);
|
||||
g_free(riff->incomplete_chunk);
|
||||
riff->incomplete_chunk = NULL;
|
||||
}
|
||||
else {
|
||||
debug("gst_riff_parser: we cannot fill it %08x >= %08lx\n", leftover, size);
|
||||
memcpy(riff->incomplete_chunk->data+riff->incomplete_chunk_size, GST_BUFFER_DATA(buf), size);
|
||||
riff->incomplete_chunk_size += size;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if ((riff->nextlikely+12) > last) {
|
||||
guint left = last - riff->nextlikely;
|
||||
debug("gst_riff_parser: not enough data next 0x%08x last 0x%08lx %08x %08x\n",riff->nextlikely,
|
||||
last, left, off);
|
||||
|
||||
riff->dataleft = g_malloc(left);
|
||||
riff->dataleft_size = left;
|
||||
memcpy(riff->dataleft, GST_BUFFER_DATA(buf)+size-left, left);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
debug("gst_riff_parser: next 0x%08x last 0x%08lx offset %08x\n",riff->nextlikely, last, off);
|
||||
/* loop while the next likely chunk header is in this buffer */
|
||||
while ((riff->nextlikely+12) <= last) {
|
||||
gulong *words = (gulong *)((guchar *)GST_BUFFER_DATA(buf) + riff->nextlikely - off );
|
||||
|
||||
// loop over all of the chunks to check which one is finished
|
||||
while (riff->chunks) {
|
||||
chunk = g_list_nth_data(riff->chunks, 0);
|
||||
|
||||
debug("gst_riff_parser: next 0x%08x offset 0x%08lx size 0x%08x\n",riff->nextlikely,
|
||||
chunk->offset, chunk->size);
|
||||
if (riff->nextlikely >= chunk->offset+chunk->size) {
|
||||
debug("gst_riff_parser: found END LIST\n");
|
||||
// we have the end of the chunk on the stack, remove it
|
||||
riff->chunks = g_list_remove(riff->chunks, chunk);
|
||||
}
|
||||
else break;
|
||||
}
|
||||
|
||||
debug("gst_riff_parser: next likely chunk is at offset 0x%08x\n",riff->nextlikely);
|
||||
|
||||
chunk = (GstRiffChunk *)g_malloc(sizeof(GstRiffChunk));
|
||||
g_return_val_if_fail(chunk != NULL, GST_RIFF_ENOMEM);
|
||||
|
||||
chunk->offset = riff->nextlikely+8; /* point to the actual data */
|
||||
chunk->id = words[0];
|
||||
chunk->size = words[1];
|
||||
chunk->data = (gchar *)(words+2);
|
||||
// we need word alignment
|
||||
//if (chunk->size & 0x01) chunk->size++;
|
||||
chunk->form = words[2]; /* fill in the form, might not be valid */
|
||||
|
||||
|
||||
if (chunk->id == GST_RIFF_TAG_LIST) {
|
||||
//g_print("found LIST %s\n", gst_riff_id_to_fourcc(chunk->form));
|
||||
riff->nextlikely += 12;
|
||||
// we push the list chunk on our 'stack'
|
||||
riff->chunks = g_list_prepend(riff->chunks,chunk);
|
||||
// send the buffer to the listener if we have received a function
|
||||
if (riff->new_tag_found) {
|
||||
riff->new_tag_found(chunk, riff->callback_data);
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
debug("gst_riff_parser: chunk id offset %08x is 0x%08lx '%s' and is 0x%08lx long\n",
|
||||
riff->nextlikely, words[0],
|
||||
gst_riff_id_to_fourcc(words[0]),words[1]);
|
||||
|
||||
riff->nextlikely += 8 + chunk->size; /* doesn't include hdr */
|
||||
// if this buffer is incomplete
|
||||
if (riff->nextlikely > last) {
|
||||
guint left = size - (riff->nextlikely - chunk->size - off);
|
||||
|
||||
//g_print("make incomplete buffer %08x\n", left);
|
||||
chunk->data = g_malloc(chunk->size);
|
||||
memcpy(chunk->data, (gchar *)(words+2), left);
|
||||
riff->incomplete_chunk = chunk;
|
||||
riff->incomplete_chunk_size = left;
|
||||
}
|
||||
else {
|
||||
// send the buffer to the listener if we have received a function
|
||||
if (riff->new_tag_found) {
|
||||
riff->new_tag_found(chunk, riff->callback_data);
|
||||
}
|
||||
g_free(chunk);
|
||||
}
|
||||
|
||||
//riff->chunks = g_list_prepend(riff->chunks,chunk);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
46
libs/riff/gstriffutil.c
Normal file
46
libs/riff/gstriffutil.c
Normal file
|
@ -0,0 +1,46 @@
|
|||
/* Gnome-Streamer
|
||||
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
|
||||
*
|
||||
* 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., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
|
||||
#include <gstriff.h>
|
||||
|
||||
//#define debug(format,args...) g_print(format,##args)
|
||||
#define debug(format,args...)
|
||||
|
||||
|
||||
gulong gst_riff_fourcc_to_id(gchar *fourcc) {
|
||||
g_return_val_if_fail(fourcc != NULL, 0);
|
||||
|
||||
return (fourcc[0] << 0) | (fourcc[1] << 8) |
|
||||
(fourcc[2] << 16) | (fourcc[3] << 24);
|
||||
}
|
||||
|
||||
gchar *gst_riff_id_to_fourcc(gulong id) {
|
||||
gchar *fourcc = (gchar *)g_malloc(5);
|
||||
|
||||
g_return_val_if_fail(fourcc != NULL, NULL);
|
||||
|
||||
fourcc[0] = (id >> 0) & 0xff;
|
||||
fourcc[1] = (id >> 8) & 0xff;
|
||||
fourcc[2] = (id >> 16) & 0xff;
|
||||
fourcc[3] = (id >> 24) & 0xff;
|
||||
fourcc[4] = 0;
|
||||
|
||||
return fourcc;
|
||||
}
|
Loading…
Reference in a new issue