Skip to content

Commit a606f27

Browse files
Cigaesjeeb
authored andcommitted
lavf/avio: temporarily accept 0 as EOF.
Print a warning to let applicatios fix their use. After a deprecation period, check with a low-level assert. Also make the constraint explicit in the doxygen comment. Signed-off-by: Nicolas George <george@nsup.org>
1 parent bfb1a94 commit a606f27

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

libavformat/avio.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,8 @@ void avio_free_directory_entry(AVIODirEntry **entry);
452452
* @param write_flag Set to 1 if the buffer should be writable, 0 otherwise.
453453
* @param opaque An opaque pointer to user-specific data.
454454
* @param read_packet A function for refilling the buffer, may be NULL.
455+
* For stream protocols, must never return 0 but rather
456+
* a proper AVERROR code.
455457
* @param write_packet A function for writing the buffer contents, may be NULL.
456458
* The function may not change the input buffers content.
457459
* @param seek A function for seeking to specified byte position, may be NULL.

libavformat/aviobuf.c

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,24 @@ void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType typ
524524
s->last_time = time;
525525
}
526526

527+
static int read_packet_wrapper(AVIOContext *s, uint8_t *buf, int size)
528+
{
529+
int ret;
530+
531+
if (!s->read_packet)
532+
return AVERROR_EOF;
533+
ret = s->read_packet(s->opaque, buf, size);
534+
#if FF_API_OLD_AVIO_EOF_0
535+
if (!ret && !s->max_packet_size) {
536+
av_log(NULL, AV_LOG_WARNING, "Invalid return value 0 for stream protocol\n");
537+
ret = AVERROR_EOF;
538+
}
539+
#else
540+
av_assert2(ret || s->max_packet_size);
541+
#endif
542+
return ret;
543+
}
544+
527545
/* Input stream */
528546

529547
static void fill_buffer(AVIOContext *s)
@@ -562,10 +580,7 @@ static void fill_buffer(AVIOContext *s)
562580
len = s->orig_buffer_size;
563581
}
564582

565-
if (s->read_packet)
566-
len = s->read_packet(s->opaque, dst, len);
567-
else
568-
len = AVERROR_EOF;
583+
len = read_packet_wrapper(s, dst, len);
569584
if (len == AVERROR_EOF) {
570585
/* do not modify buffer if EOF reached so that a seek back can
571586
be done without rereading data */
@@ -638,10 +653,7 @@ int avio_read(AVIOContext *s, unsigned char *buf, int size)
638653
if (len == 0 || s->write_flag) {
639654
if((s->direct || size > s->buffer_size) && !s->update_checksum) {
640655
// bypass the buffer and read data directly into buf
641-
if(s->read_packet)
642-
len = s->read_packet(s->opaque, buf, size);
643-
else
644-
len = AVERROR_EOF;
656+
len = read_packet_wrapper(s, buf, size);
645657
if (len == AVERROR_EOF) {
646658
/* do not modify buffer if EOF reached so that a seek back can
647659
be done without rereading data */
@@ -708,7 +720,7 @@ int avio_read_partial(AVIOContext *s, unsigned char *buf, int size)
708720
return -1;
709721

710722
if (s->read_packet && s->write_flag) {
711-
len = s->read_packet(s->opaque, buf, size);
723+
len = read_packet_wrapper(s, buf, size);
712724
if (len > 0)
713725
s->pos += len;
714726
return len;

libavformat/version.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@
7979
#ifndef FF_API_FORMAT_GET_SET
8080
#define FF_API_FORMAT_GET_SET (LIBAVFORMAT_VERSION_MAJOR < 59)
8181
#endif
82+
#ifndef FF_API_OLD_AVIO_EOF_0
83+
#define FF_API_OLD_AVIO_EOF_0 (LIBAVFORMAT_VERSION_MAJOR < 59)
84+
#endif
8285

8386

8487
#ifndef FF_API_R_FRAME_RATE

0 commit comments

Comments
 (0)