summaryrefslogtreecommitdiff
path: root/ext/musepack/gstmusepackreader.c
blob: 6717698f73e995a0a978bb1b45a2eb760c3646d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* GStreamer Musepack decoder plugin
 * Copyright (C) 2004 Ronald Bultje <rbultje@ronald.bitfreak.net>
 *
 * 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., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "gstmusepackreader.h"
#include <gst/gst.h>
#include <string.h>


GST_DEBUG_CATEGORY_EXTERN (musepackdec_debug);
#define GST_CAT_DEFAULT musepackdec_debug

#ifdef MPC_IS_OLD_API
static mpc_int32_t gst_musepack_reader_peek (void *this, void *ptr,
    mpc_int32_t size);
static mpc_int32_t gst_musepack_reader_read (void *this, void *ptr,
    mpc_int32_t size);
static mpc_bool_t gst_musepack_reader_seek (void *this, mpc_int32_t offset);
static mpc_int32_t gst_musepack_reader_tell (void *this);
static mpc_int32_t gst_musepack_reader_get_size (void *this);
static mpc_bool_t gst_musepack_reader_canseek (void *this);
#else
static mpc_int32_t gst_musepack_reader_peek (mpc_reader * this, void *ptr,
    mpc_int32_t size);
static mpc_int32_t gst_musepack_reader_read (mpc_reader * this, void *ptr,
    mpc_int32_t size);
static mpc_bool_t gst_musepack_reader_seek (mpc_reader * this,
    mpc_int32_t offset);
static mpc_int32_t gst_musepack_reader_tell (mpc_reader * this);
static mpc_int32_t gst_musepack_reader_get_size (mpc_reader * this);
static mpc_bool_t gst_musepack_reader_canseek (mpc_reader * this);
#endif

#ifdef MPC_IS_OLD_API
static mpc_int32_t
gst_musepack_reader_peek (void *this, void *ptr, mpc_int32_t size)
{
  GstMusepackDec *musepackdec = GST_MUSEPACK_DEC (this);
#else
static mpc_int32_t
gst_musepack_reader_peek (mpc_reader * this, void *ptr, mpc_int32_t size)
{
  GstMusepackDec *musepackdec = GST_MUSEPACK_DEC (this->data);
#endif
  GstFlowReturn flow_ret;
  GstBuffer *buf = NULL;
  guint read;

  g_return_val_if_fail (size > 0, 0);

  /* GST_LOG_OBJECT (musepackdec, "size=%d", size); */

  flow_ret = gst_pad_pull_range (musepackdec->sinkpad, musepackdec->offset,
      size, &buf);

  if (flow_ret != GST_FLOW_OK) {
    GST_DEBUG_OBJECT (musepackdec, "Flow: %s", gst_flow_get_name (flow_ret));
    return 0;
  }

  read = MIN (GST_BUFFER_SIZE (buf), size);

  if (read < size) {
    GST_WARNING_OBJECT (musepackdec, "Short read: got only %u bytes of %u "
        "bytes requested at offset %" G_GINT64_FORMAT, read, size,
        musepackdec->offset);
    /* GST_ELEMENT_ERROR (musepackdec, RESOURCE, READ, (NULL), (NULL)); */
  }

  memcpy (ptr, GST_BUFFER_DATA (buf), read);
  gst_buffer_unref (buf);
  return read;
}

#ifdef MPC_IS_OLD_API
static mpc_int32_t
gst_musepack_reader_read (void *this, void *ptr, mpc_int32_t size)
{
  GstMusepackDec *musepackdec = GST_MUSEPACK_DEC (this);
#else
static mpc_int32_t
gst_musepack_reader_read (mpc_reader * this, void *ptr, mpc_int32_t size)
{
  GstMusepackDec *musepackdec = GST_MUSEPACK_DEC (this->data);
#endif
  gint read;

  /* read = peek + flush */
  if ((read = gst_musepack_reader_peek (this, ptr, size)) > 0) {
    musepackdec->offset += read;
  }

  return read;
}

#ifdef MPC_IS_OLD_API
static mpc_bool_t
gst_musepack_reader_seek (void *this, mpc_int32_t offset)
{
  GstMusepackDec *musepackdec = GST_MUSEPACK_DEC (this);
#else
static mpc_bool_t
gst_musepack_reader_seek (mpc_reader * this, mpc_int32_t offset)
{
  GstMusepackDec *musepackdec = GST_MUSEPACK_DEC (this->data);
#endif
  mpc_int32_t length;

  length = gst_musepack_reader_get_size (this);
  if (length > 0 && offset >= 0 && offset < length) {
    musepackdec->offset = offset;
    GST_LOG_OBJECT (musepackdec, "Seek'ed to byte offset %d", (gint) offset);
    return TRUE;
  } else {
    GST_DEBUG_OBJECT (musepackdec, "Cannot seek to offset %d", (gint) offset);
    return FALSE;
  }
}

#ifdef MPC_IS_OLD_API
static mpc_int32_t
gst_musepack_reader_tell (void *this)
{
  GstMusepackDec *musepackdec = GST_MUSEPACK_DEC (this);
#else
static mpc_int32_t
gst_musepack_reader_tell (mpc_reader * this)
{
  GstMusepackDec *musepackdec = GST_MUSEPACK_DEC (this->data);
#endif
  return musepackdec->offset;
}

#ifdef MPC_IS_OLD_API
static mpc_int32_t
gst_musepack_reader_get_size (void *this)
{
  GstMusepackDec *dec = GST_MUSEPACK_DEC (this);
#else
static mpc_int32_t
gst_musepack_reader_get_size (mpc_reader * this)
{
  GstMusepackDec *dec = GST_MUSEPACK_DEC (this->data);
#endif
  GstFormat format = GST_FORMAT_BYTES;
  gint64 length = -1;

  if (!gst_pad_query_peer_duration (dec->sinkpad, &format, &length))
    length = -1;

  return (mpc_int32_t) length;
}

#ifdef MPC_IS_OLD_API
static mpc_bool_t
gst_musepack_reader_canseek (void *this)
#else
static mpc_bool_t
gst_musepack_reader_canseek (mpc_reader * this)
#endif
{
  return TRUE;
}

void
gst_musepack_init_reader (mpc_reader * r, GstMusepackDec * musepackdec)
{
  r->data = musepackdec;

  r->read = gst_musepack_reader_read;
  r->seek = gst_musepack_reader_seek;
  r->tell = gst_musepack_reader_tell;
  r->get_size = gst_musepack_reader_get_size;
  r->canseek = gst_musepack_reader_canseek;
}