• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer Musepack decoder plugin
2  * Copyright (C) 2004 Ronald Bultje <rbultje@ronald.bitfreak.net>
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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include "gstmusepackreader.h"
25 #include <gst/gst.h>
26 #include <string.h>
27 
28 
29 GST_DEBUG_CATEGORY_EXTERN (musepackdec_debug);
30 #define GST_CAT_DEFAULT musepackdec_debug
31 
32 static mpc_int32_t gst_musepack_reader_peek (mpc_reader * this, void *ptr,
33     mpc_int32_t size);
34 static mpc_int32_t gst_musepack_reader_read (mpc_reader * this, void *ptr,
35     mpc_int32_t size);
36 static mpc_bool_t gst_musepack_reader_seek (mpc_reader * this,
37     mpc_int32_t offset);
38 static mpc_int32_t gst_musepack_reader_tell (mpc_reader * this);
39 static mpc_int32_t gst_musepack_reader_get_size (mpc_reader * this);
40 static mpc_bool_t gst_musepack_reader_canseek (mpc_reader * this);
41 
42 static mpc_int32_t
gst_musepack_reader_peek(mpc_reader * this,void * ptr,mpc_int32_t size)43 gst_musepack_reader_peek (mpc_reader * this, void *ptr, mpc_int32_t size)
44 {
45   GstMusepackDec *musepackdec = GST_MUSEPACK_DEC (this->data);
46   GstFlowReturn flow_ret;
47   GstBuffer *buf = NULL;
48   guint read;
49 
50   g_return_val_if_fail (size > 0, 0);
51 
52   /* GST_LOG_OBJECT (musepackdec, "size=%d", size); */
53 
54   flow_ret = gst_pad_pull_range (musepackdec->sinkpad, musepackdec->offset,
55       size, &buf);
56 
57   if (flow_ret != GST_FLOW_OK) {
58     GST_DEBUG_OBJECT (musepackdec, "Flow: %s", gst_flow_get_name (flow_ret));
59     return 0;
60   }
61 
62   read = MIN (gst_buffer_get_size (buf), size);
63 
64   if (read < size) {
65     GST_WARNING_OBJECT (musepackdec, "Short read: got only %u bytes of %u "
66         "bytes requested at offset %" G_GINT64_FORMAT, read, size,
67         musepackdec->offset);
68     /* GST_ELEMENT_ERROR (musepackdec, RESOURCE, READ, (NULL), (NULL)); */
69   }
70 
71   gst_buffer_extract (buf, 0, ptr, read);
72   gst_buffer_unref (buf);
73   return read;
74 }
75 
76 static mpc_int32_t
gst_musepack_reader_read(mpc_reader * this,void * ptr,mpc_int32_t size)77 gst_musepack_reader_read (mpc_reader * this, void *ptr, mpc_int32_t size)
78 {
79   GstMusepackDec *musepackdec = GST_MUSEPACK_DEC (this->data);
80   gint read;
81 
82   /* read = peek + flush */
83   if ((read = gst_musepack_reader_peek (this, ptr, size)) > 0) {
84     musepackdec->offset += read;
85   }
86 
87   return read;
88 }
89 
90 static mpc_bool_t
gst_musepack_reader_seek(mpc_reader * this,mpc_int32_t offset)91 gst_musepack_reader_seek (mpc_reader * this, mpc_int32_t offset)
92 {
93   GstMusepackDec *musepackdec = GST_MUSEPACK_DEC (this->data);
94   mpc_int32_t length;
95 
96   length = gst_musepack_reader_get_size (this);
97   if (length > 0 && offset >= 0 && offset < length) {
98     musepackdec->offset = offset;
99     GST_LOG_OBJECT (musepackdec, "Seek'ed to byte offset %d", (gint) offset);
100     return TRUE;
101   } else {
102     GST_DEBUG_OBJECT (musepackdec, "Cannot seek to offset %d", (gint) offset);
103     return FALSE;
104   }
105 }
106 
107 static mpc_int32_t
gst_musepack_reader_tell(mpc_reader * this)108 gst_musepack_reader_tell (mpc_reader * this)
109 {
110   GstMusepackDec *musepackdec = GST_MUSEPACK_DEC (this->data);
111   return musepackdec->offset;
112 }
113 
114 static mpc_int32_t
gst_musepack_reader_get_size(mpc_reader * this)115 gst_musepack_reader_get_size (mpc_reader * this)
116 {
117   GstMusepackDec *dec = GST_MUSEPACK_DEC (this->data);
118   GstQuery *query;
119   GstFormat format;
120   gint64 length;
121 
122   query = gst_query_new_duration (GST_FORMAT_BYTES);
123   if (gst_pad_peer_query (dec->sinkpad, query))
124     gst_query_parse_duration (query, &format, &length);
125   else
126     length = -1;
127 
128   return (mpc_int32_t) length;
129 }
130 
131 static mpc_bool_t
gst_musepack_reader_canseek(mpc_reader * this)132 gst_musepack_reader_canseek (mpc_reader * this)
133 {
134   return TRUE;
135 }
136 
137 void
gst_musepack_init_reader(mpc_reader * r,GstMusepackDec * musepackdec)138 gst_musepack_init_reader (mpc_reader * r, GstMusepackDec * musepackdec)
139 {
140   r->data = musepackdec;
141 
142   r->read = gst_musepack_reader_read;
143   r->seek = gst_musepack_reader_seek;
144   r->tell = gst_musepack_reader_tell;
145   r->get_size = gst_musepack_reader_get_size;
146   r->canseek = gst_musepack_reader_canseek;
147 }
148