Skip to content

Commit b74735e

Browse files
rheniumYuudai Yamashigi
authored and
Yuudai Yamashigi
committed
aribcam: refactor Read()
* First try of p_sys->p_b25->get() always does nothing, so read from stream first. * Merge DecoderRead() into Read() Signed-off-by: Yuudai Yamashigi <[email protected]>
1 parent 89f119a commit b74735e

File tree

1 file changed

+30
-47
lines changed

1 file changed

+30
-47
lines changed

modules/stream_filter/aribcam.c

Lines changed: 30 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,14 @@ static void RemainFlush( stream_sys_t *p_sys )
163163

164164
#define ALL_READY (UNIT_SIZE_READY|ECM_READY|PMT_READY)
165165

166-
static int DecoderRead( stream_t *p_stream, uint8_t *p_dst, int i_toread )
166+
static ssize_t Read( stream_t *p_stream, void *p_buf, size_t i_toread )
167167
{
168168
stream_sys_t *p_sys = p_stream->p_sys;
169-
ARIB_STD_B25_BUFFER getbuf = { NULL, 0 };
169+
uint8_t *p_dst = p_buf;
170170
int i_total_read = 0;
171171
int i_ret;
172172

173-
if ( !p_dst || ! i_toread )
173+
if ( !p_dst || !i_toread )
174174
return -1;
175175

176176
/* Use data from previous reads */
@@ -179,71 +179,54 @@ static int DecoderRead( stream_t *p_stream, uint8_t *p_dst, int i_toread )
179179
p_dst += i_fromremain;
180180
i_toread -= i_fromremain;
181181

182-
while( i_toread )
182+
while ( i_toread )
183183
{
184-
185-
do
184+
/* make use of the existing buffer, overwritten by decoder data later */
185+
int i_srcread = stream_Read( p_stream->p_source, p_dst, i_toread );
186+
if ( i_srcread > 0 )
186187
{
187-
getbuf.size = 0;
188-
189-
i_ret = p_sys->p_b25->get( p_sys->p_b25, &getbuf );
188+
ARIB_STD_B25_BUFFER putbuf = { p_dst, i_srcread };
189+
i_ret = p_sys->p_b25->put( p_sys->p_b25, &putbuf );
190190
if ( i_ret < 0 )
191-
msg_Err( p_stream, "decoder get failed: %s",
192-
GetErrorMessage( i_ret, b25_errors ) );
193-
194-
/* If the decoders needs buffering or data is not ready, push some */
195-
if ( i_ret == 0 && getbuf.size == 0 )
196191
{
197-
/* make use of the existing buffer,
198-
overwritten by decoder data later */
199-
int i_srcread = stream_Read( p_stream->p_source, p_dst, i_toread );
200-
if ( i_srcread > 0 )
201-
{
202-
ARIB_STD_B25_BUFFER putbuf = { p_dst, i_srcread };
203-
i_ret = p_sys->p_b25->put( p_sys->p_b25, &putbuf );
204-
if ( i_ret < 0 )
205-
msg_Err( p_stream, "decoder put failed: %s",
206-
GetErrorMessage( i_ret, b25_errors ) );
207-
}
208-
else
209-
{
210-
if ( i_srcread < 0 )
211-
msg_Err( p_stream, "Can't read %d bytes from source stream: %d", i_toread, i_srcread );
212-
i_ret = -1;
213-
}
192+
msg_Err( p_stream, "decoder put failed: %s",
193+
GetErrorMessage( i_ret, b25_errors ) );
194+
return -1;
214195
}
215196
}
216-
while ( i_ret == 0 && getbuf.size == 0 );
197+
else
198+
{
199+
if ( i_srcread < 0 )
200+
msg_Err( p_stream, "Can't read %lu bytes from source stream: %d", i_toread, i_srcread );
201+
return -1;
202+
}
217203

204+
ARIB_STD_B25_BUFFER getbuf;
205+
i_ret = p_sys->p_b25->get( p_sys->p_b25, &getbuf );
218206
if ( i_ret < 0 )
207+
{
208+
msg_Err( p_stream, "decoder get failed: %s",
209+
GetErrorMessage( i_ret, b25_errors ) );
219210
return -1;
211+
}
220212

221-
memcpy( p_dst, getbuf.data, __MIN(getbuf.size, i_toread) );
222-
223-
if ( getbuf.size > i_toread )
213+
if ( (size_t)getbuf.size > i_toread )
224214
{
225215
/* Hold remaining data for next call */
226216
RemainAdd( p_stream, getbuf.data + i_toread, getbuf.size - i_toread );
227217
}
228218

229-
i_total_read += __MIN(getbuf.size, i_toread);
230-
p_dst += __MIN(getbuf.size, i_toread);
231-
i_toread -= __MIN(getbuf.size, i_toread);
219+
int consume = __MIN( (size_t)getbuf.size, i_toread );
220+
memcpy( p_dst, getbuf.data, consume );
232221

222+
i_total_read += consume;
223+
p_dst += consume;
224+
i_toread -= consume;
233225
}
234226

235227
return i_total_read;
236228
}
237229

238-
static ssize_t Read( stream_t *p_stream, void *p_buf, size_t i_toread )
239-
{
240-
stream_sys_t *p_sys = p_stream->p_sys;
241-
int i_read = DecoderRead( p_stream, p_buf, i_toread );
242-
if ( i_read < 0 )
243-
return -1;
244-
return i_read;
245-
}
246-
247230
/**
248231
*
249232
*/

0 commit comments

Comments
 (0)