Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / ext / wavpack / gstwavpackstreamreader.c
1 /* GStreamer Wavpack plugin
2  * Copyright (c) 2006 Sebastian Dröge <slomo@circular-chaos.org>
3  *
4  * gstwavpackstreamreader.c: stream reader used for decoding
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include <string.h>
23 #include <math.h>
24 #include <gst/gst.h>
25
26 #include "gstwavpackstreamreader.h"
27
28 GST_DEBUG_CATEGORY_EXTERN (wavpack_debug);
29 #define GST_CAT_DEFAULT wavpack_debug
30
31 static int32_t
32 gst_wavpack_stream_reader_read_bytes (void *id, void *data, int32_t bcount)
33 {
34   read_id *rid = (read_id *) id;
35   uint32_t left = rid->length - rid->position;
36   uint32_t to_read = MIN (left, bcount);
37
38   GST_DEBUG ("Trying to read %d of %d bytes from position %d", bcount,
39       rid->length, rid->position);
40
41   if (to_read > 0) {
42     g_memmove (data, rid->buffer + rid->position, to_read);
43     rid->position += to_read;
44     return to_read;
45   } else {
46     GST_WARNING ("Couldn't read %d bytes", bcount);
47     return 0;
48   }
49 }
50
51 static uint32_t
52 gst_wavpack_stream_reader_get_pos (void *id)
53 {
54   GST_DEBUG ("Returning position %d", ((read_id *) id)->position);
55   return ((read_id *) id)->position;
56 }
57
58 static int
59 gst_wavpack_stream_reader_set_pos_abs (void *id, uint32_t pos)
60 {
61   GST_WARNING ("Should not be called: tried to set absolute position to %d",
62       pos);
63   return -1;
64 }
65
66 static int
67 gst_wavpack_stream_reader_set_pos_rel (void *id, int32_t delta, int mode)
68 {
69   GST_WARNING ("Should not be called: tried to set relative position to %d"
70       " with mode %d", delta, mode);
71   return -1;
72 }
73
74 static int
75 gst_wavpack_stream_reader_push_back_byte (void *id, int c)
76 {
77   read_id *rid = (read_id *) id;
78
79   GST_DEBUG ("Pushing back one byte: 0x%x", c);
80
81   rid->position -= 1;
82   if (rid->position < 0)
83     rid->position = 0;
84   return rid->position;
85 }
86
87 static uint32_t
88 gst_wavpack_stream_reader_get_length (void *id)
89 {
90   GST_DEBUG ("Returning length %d", ((read_id *) id)->length);
91
92   return ((read_id *) id)->length;
93 }
94
95 static int
96 gst_wavpack_stream_reader_can_seek (void *id)
97 {
98   GST_DEBUG ("Can't seek");
99   return FALSE;
100 }
101
102 static int32_t
103 gst_wavpack_stream_reader_write_bytes (void *id, void *data, int32_t bcount)
104 {
105   GST_WARNING ("Should not be called, tried to write %d bytes", bcount);
106   return 0;
107 }
108
109 WavpackStreamReader *
110 gst_wavpack_stream_reader_new (void)
111 {
112   WavpackStreamReader *stream_reader =
113       (WavpackStreamReader *) g_malloc0 (sizeof (WavpackStreamReader));
114   stream_reader->read_bytes = gst_wavpack_stream_reader_read_bytes;
115   stream_reader->get_pos = gst_wavpack_stream_reader_get_pos;
116   stream_reader->set_pos_abs = gst_wavpack_stream_reader_set_pos_abs;
117   stream_reader->set_pos_rel = gst_wavpack_stream_reader_set_pos_rel;
118   stream_reader->push_back_byte = gst_wavpack_stream_reader_push_back_byte;
119   stream_reader->get_length = gst_wavpack_stream_reader_get_length;
120   stream_reader->can_seek = gst_wavpack_stream_reader_can_seek;
121   stream_reader->write_bytes = gst_wavpack_stream_reader_write_bytes;
122
123   return stream_reader;
124 }