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., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, 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
gst_wavpack_stream_reader_read_bytes(void * id,void * data,int32_t bcount)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     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
gst_wavpack_stream_reader_get_pos(void * id)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
gst_wavpack_stream_reader_set_pos_abs(void * id,uint32_t pos)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
gst_wavpack_stream_reader_set_pos_rel(void * id,int32_t delta,int mode)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
gst_wavpack_stream_reader_push_back_byte(void * id,int c)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   if (rid->position == 0)
82     return rid->position;
83 
84   rid->position -= 1;
85   return rid->position;
86 }
87 
88 static uint32_t
gst_wavpack_stream_reader_get_length(void * id)89 gst_wavpack_stream_reader_get_length (void *id)
90 {
91   GST_DEBUG ("Returning length %d", ((read_id *) id)->length);
92 
93   return ((read_id *) id)->length;
94 }
95 
96 static int
gst_wavpack_stream_reader_can_seek(void * id)97 gst_wavpack_stream_reader_can_seek (void *id)
98 {
99   GST_DEBUG ("Can't seek");
100   return FALSE;
101 }
102 
103 static int32_t
gst_wavpack_stream_reader_write_bytes(void * id,void * data,int32_t bcount)104 gst_wavpack_stream_reader_write_bytes (void *id, void *data, int32_t bcount)
105 {
106   GST_WARNING ("Should not be called, tried to write %d bytes", bcount);
107   return 0;
108 }
109 
110 WavpackStreamReader *
gst_wavpack_stream_reader_new(void)111 gst_wavpack_stream_reader_new (void)
112 {
113   WavpackStreamReader *stream_reader =
114       (WavpackStreamReader *) g_malloc0 (sizeof (WavpackStreamReader));
115   stream_reader->read_bytes = gst_wavpack_stream_reader_read_bytes;
116   stream_reader->get_pos = gst_wavpack_stream_reader_get_pos;
117   stream_reader->set_pos_abs = gst_wavpack_stream_reader_set_pos_abs;
118   stream_reader->set_pos_rel = gst_wavpack_stream_reader_set_pos_rel;
119   stream_reader->push_back_byte = gst_wavpack_stream_reader_push_back_byte;
120   stream_reader->get_length = gst_wavpack_stream_reader_get_length;
121   stream_reader->can_seek = gst_wavpack_stream_reader_can_seek;
122   stream_reader->write_bytes = gst_wavpack_stream_reader_write_bytes;
123 
124   return stream_reader;
125 }
126