]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/gst-plugins-ugly0-10.git/blob - gst-libs/gst/riff/riffencode.c
a6caed9ee54af7f1e56d3bba441a77bfb83ba47b
[glsdk/gst-plugins-ugly0-10.git] / gst-libs / gst / riff / riffencode.c
1 /* Gnome-Streamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
20 #include <string.h>
22 //#define DEBUG_ENABLED
23 #include "riff.h"
25 #define GST_RIFF_ENCODER_BUF_SIZE    1024
27 #define ADD_CHUNK(riffenc, chunkid, chunksize) \
28 { \
29   gst_riff_chunk *chunk;\
30   chunk = (gst_riff_chunk *)(riffenc->dataleft + riffenc->nextlikely);\
31   chunk->id = chunkid; \
32   chunk->size = chunksize; \
33   riffenc->nextlikely += sizeof(gst_riff_chunk); \
34 }
36 #define ADD_LIST(riffenc, listsize, listtype) \
37 { \
38   gst_riff_list *list;\
39   list = (gst_riff_list *)(riffenc->dataleft + riffenc->nextlikely); \
40   list->id = GST_RIFF_TAG_LIST; \
41   list->size = listsize; \
42   list->type = listtype; \
43   riffenc->nextlikely += sizeof(gst_riff_list); \
44 }
45   
47 GstRiff *gst_riff_encoder_new(guint32 type) {
48   GstRiff *riff;
49   gst_riff_list *list;
51   GST_DEBUG (0,"gst_riff_encoder: making %4.4s encoder\n", (char *)&type);
52   riff = (GstRiff *)g_malloc(sizeof(GstRiff));
53   g_return_val_if_fail(riff != NULL, NULL);
55   riff->form = 0;
56   riff->chunks = NULL;
57   riff->state = GST_RIFF_STATE_INITIAL;
58   riff->curoffset = 0;
59   riff->incomplete_chunk = NULL;
60   riff->dataleft = g_malloc(GST_RIFF_ENCODER_BUF_SIZE);
61   riff->dataleft_size = GST_RIFF_ENCODER_BUF_SIZE;
62   riff->nextlikely = 0;
64   list = (gst_riff_list *)riff->dataleft;
65   list->id = GST_RIFF_TAG_RIFF;
66   list->size = 0x00FFFFFF;
67   list->type = GST_RIFF_RIFF_AVI;
69   riff->nextlikely += sizeof(gst_riff_list);
70   
71   return riff;
72 }
74 gint gst_riff_encoder_avih(GstRiff *riff, gst_riff_avih *head, gulong size) {
75   gst_riff_chunk *chunk;
77   g_return_val_if_fail(riff->state == GST_RIFF_STATE_INITIAL, GST_RIFF_EINVAL);
79   GST_DEBUG (0,"gst_riff_encoder: add avih\n");
81   ADD_LIST(riff, 0xB8, GST_RIFF_LIST_hdrl);
83   ADD_CHUNK(riff, GST_RIFF_TAG_avih, size);
84   
85   chunk = (gst_riff_chunk *)(riff->dataleft + riff->nextlikely);
86   memcpy(chunk, head, size);
87   riff->nextlikely += size;
89   riff->state = GST_RIFF_STATE_HASAVIH;
90   return GST_RIFF_OK;
91 }
93 gint gst_riff_encoder_strh(GstRiff *riff, guint32 fcc_type, gst_riff_strh *head, gulong size) {
94   gst_riff_chunk *chunk;
96   g_return_val_if_fail(riff->state == GST_RIFF_STATE_HASAVIH ||
97                        riff->state == GST_RIFF_STATE_HASSTRF, GST_RIFF_EINVAL);
99   GST_DEBUG (0,"gst_riff_encoder: add strh type %08x (%4.4s)\n", fcc_type, (char *)&fcc_type);
101   ADD_LIST(riff, 108, GST_RIFF_LIST_strl);
103   ADD_CHUNK(riff, GST_RIFF_TAG_strh, size);
105   chunk = (gst_riff_chunk *)(riff->dataleft + riff->nextlikely);
106   head->type = fcc_type;
107   memcpy(chunk, head, size);
109   riff->nextlikely += size;
111   riff->state = GST_RIFF_STATE_HASSTRH;
112   return GST_RIFF_OK;
115 gint gst_riff_encoder_strf(GstRiff *riff, void *format, gulong size) {
116   gst_riff_chunk *chunk;
118   g_return_val_if_fail(riff->state == GST_RIFF_STATE_HASSTRH, GST_RIFF_EINVAL);
120   GST_DEBUG (0,"gst_riff_encoder: add strf\n");
122   ADD_CHUNK(riff, GST_RIFF_TAG_strf, size);
124   chunk = (gst_riff_chunk *)(riff->dataleft + riff->nextlikely);
125   memcpy(chunk, format, size);
126   riff->nextlikely += size;
128   riff->state = GST_RIFF_STATE_HASSTRF;
129   return GST_RIFF_OK;
132 gint gst_riff_encoder_chunk(GstRiff *riff, guint32 chunk_type, void *chunkdata, gulong size) {
133   gst_riff_chunk *chunk;
135   g_return_val_if_fail(riff->state == GST_RIFF_STATE_HASSTRF ||
136                        riff->state == GST_RIFF_STATE_MOVI, GST_RIFF_EINVAL);
138   if (riff->state != GST_RIFF_STATE_MOVI) {
139     ADD_LIST(riff, 0x00FFFFFF, GST_RIFF_LIST_movi);
140     riff->state = GST_RIFF_STATE_MOVI;
141   }
143   GST_DEBUG (0,"gst_riff_encoder: add chunk type %08x (%4.4s)\n", chunk_type, (char *)&chunk_type);
144   
145   ADD_CHUNK(riff, chunk_type, size);
147   if (chunkdata != NULL) {
148     chunk = (gst_riff_chunk *)(riff->dataleft + riff->nextlikely);
149     memcpy(chunk, chunkdata, size);
150     riff->nextlikely += size + (size&1);
151   }
153   return GST_RIFF_OK;
156 GstBuffer *gst_riff_encoder_get_buffer(GstRiff *riff) {
157   GstBuffer *newbuf;
159   newbuf = gst_buffer_new();
160   GST_BUFFER_DATA(newbuf) = riff->dataleft;
161   GST_BUFFER_SIZE(newbuf) = riff->nextlikely;
163   return newbuf;
166 GstBuffer *gst_riff_encoder_get_and_reset_buffer(GstRiff *riff) {
167   GstBuffer *newbuf;
169   newbuf = gst_riff_encoder_get_buffer(riff);
171   riff->dataleft = g_malloc(GST_RIFF_ENCODER_BUF_SIZE);
172   riff->dataleft_size = GST_RIFF_ENCODER_BUF_SIZE;
173   riff->nextlikely = 0;
175   return newbuf;