Return AVERROR_EOF from the avio_read method when appropriate.
authorCarl Hetherington <cth@carlh.net>
Sat, 1 Oct 2022 18:11:40 +0000 (20:11 +0200)
committerCarl Hetherington <cth@carlh.net>
Fri, 25 Nov 2022 23:09:27 +0000 (00:09 +0100)
src/lib/ffmpeg.cc
src/lib/file_group.cc
src/lib/file_group.h
test/file_group_test.cc

index 6ec368ef64f49b7fabf3788ac95a8f655ebdfd34..39abfe2b84ce71679cd66af9ac20a7d59a34d199 100644 (file)
@@ -273,7 +273,11 @@ FFmpeg::subtitle_codec_context () const
 int
 FFmpeg::avio_read (uint8_t* buffer, int const amount)
 {
-       return _file_group.read (buffer, amount);
+       auto result = _file_group.read(buffer, amount);
+       if (result.eof && result.bytes_read == 0) {
+               return AVERROR_EOF;
+       }
+       return result.bytes_read;
 }
 
 
index 71faacc4c0fa6bd27667c761c873ddce42d4ec88..f4d50028ecdc1037ae1547818e30248992fd64d4 100644 (file)
@@ -134,9 +134,8 @@ FileGroup::seek (int64_t pos, int whence) const
 /** Try to read some data from the current position into a buffer.
  *  @param buffer Buffer to write data into.
  *  @param amount Number of bytes to read.
- *  @return Number of bytes read.
  */
-int
+FileGroup::Result
 FileGroup::read (uint8_t* buffer, int amount) const
 {
        DCPOMATIC_ASSERT (_current_file);
@@ -173,13 +172,13 @@ FileGroup::read (uint8_t* buffer, int amount) const
                if (eof) {
                        /* See if there is another file to use */
                        if ((_current_path + 1) >= _paths.size()) {
-                               break;
+                               return { read, true };
                        }
                        ensure_open_path (_current_path + 1);
                }
        }
 
-       return read;
+       return { read, false };
 }
 
 
index 693bf89baeb800757fa61acfdeccb243eeb02ae5..aac72c2288ef1d394bd681059c8d2e07adfdbf3f 100644 (file)
@@ -49,8 +49,18 @@ public:
 
        void set_paths (std::vector<boost::filesystem::path> const &);
 
+       struct Result {
+               Result(int bytes_read_, bool eof_)
+                       : bytes_read(bytes_read_)
+                       , eof(eof_)
+               {}
+
+               int bytes_read = 0;
+               bool eof = false;
+       };
+
        int64_t seek (int64_t, int) const;
-       int read (uint8_t*, int) const;
+       Result read (uint8_t*, int) const;
        int64_t length () const;
 
 private:
index f974a37256b3ae19ed6f71f28e4fba1cbb066f36..3fb31317c93688eaab8c5f96cf7acca46c510ec6 100644 (file)
@@ -79,51 +79,53 @@ BOOST_AUTO_TEST_CASE (file_group_test)
        int pos = 0;
 
        /* Basic read from 0 */
-       BOOST_CHECK_EQUAL (fg.read(test, 64), 64);
+       BOOST_CHECK_EQUAL(fg.read(test, 64).bytes_read, 64);
        BOOST_CHECK_EQUAL (memcmp(data, test, 64), 0);
        pos += 64;
 
        /* Another read following the previous */
-       BOOST_CHECK_EQUAL (fg.read(test, 4), 4);
+       BOOST_CHECK_EQUAL(fg.read(test, 4).bytes_read, 4);
        BOOST_CHECK_EQUAL (memcmp(data + pos, test, 4), 0);
        pos += 4;
 
        /* Read overlapping A and B */
-       BOOST_CHECK_EQUAL (fg.read(test, 128), 128);
+       BOOST_CHECK_EQUAL(fg.read(test, 128).bytes_read, 128);
        BOOST_CHECK_EQUAL (memcmp(data + pos, test, 128), 0);
        pos += 128;
 
        /* Read overlapping B/C/D and over-reading by a lot */
-       BOOST_CHECK_EQUAL (fg.read(test, total_length * 3), total_length - pos);
+       BOOST_CHECK_EQUAL(fg.read(test, total_length * 3).bytes_read, total_length - pos);
        BOOST_CHECK_EQUAL (memcmp(data + pos, test, total_length - pos), 0);
 
        /* Over-read by a little */
        BOOST_CHECK_EQUAL (fg.seek(0, SEEK_SET), 0);
-       BOOST_CHECK_EQUAL (fg.read(test, total_length), total_length);
-       BOOST_CHECK_EQUAL (fg.read(test, 1), 0);
+       BOOST_CHECK_EQUAL(fg.read(test, total_length).bytes_read, total_length);
+       BOOST_CHECK_EQUAL(fg.read(test, 1).bytes_read, 0);
 
        /* Seeking off the end of the file should not give an error */
        BOOST_CHECK_EQUAL (fg.seek(total_length * 2, SEEK_SET), total_length * 2);
-       /* and attempting to read should return nothing */
-       BOOST_CHECK_EQUAL (fg.read(test, 64), 0);
+       /* and attempting to read should return nothing and EOF */
+       auto result = fg.read(test, 64);
+       BOOST_CHECK_EQUAL(result.bytes_read, 0);
+       BOOST_CHECK(result.eof);
        /* but the requested seek should be remembered, so if we now go back (relatively) */
        BOOST_CHECK_EQUAL (fg.seek(-total_length * 2, SEEK_CUR), 0);
        /* we should be at the start again */
-       BOOST_CHECK_EQUAL (fg.read(test, 64), 64);
+       BOOST_CHECK_EQUAL(fg.read(test, 64).bytes_read, 64);
        BOOST_CHECK_EQUAL (memcmp(data, test, 64), 0);
 
        /* SEEK_SET */
        BOOST_CHECK_EQUAL (fg.seek(999, SEEK_SET), 999);
-       BOOST_CHECK_EQUAL (fg.read(test, 64), 64);
+       BOOST_CHECK_EQUAL(fg.read(test, 64).bytes_read, 64);
        BOOST_CHECK_EQUAL (memcmp(data + 999, test, 64), 0);
 
        /* SEEK_CUR */
        BOOST_CHECK_EQUAL (fg.seek(42, SEEK_CUR), 999 + 64 + 42);
-       BOOST_CHECK_EQUAL (fg.read(test, 64), 64);
+       BOOST_CHECK_EQUAL(fg.read(test, 64).bytes_read, 64);
        BOOST_CHECK_EQUAL (memcmp(data + 999 + 64 + 42, test, 64), 0);
 
        /* SEEK_END */
        BOOST_CHECK_EQUAL (fg.seek(1077, SEEK_END), total_length - 1077);
-       BOOST_CHECK_EQUAL (fg.read(test, 256), 256);
+       BOOST_CHECK_EQUAL(fg.read(test, 256).bytes_read, 256);
        BOOST_CHECK_EQUAL (memcmp(data + total_length - 1077, test, 256), 0);
 }