Coverage Report

Created: 2026-06-10 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libheif/libheif/file_layout.h
Line
Count
Source
1
/*
2
 * HEIF codec.
3
 * Copyright (c) 2024 Dirk Farin <dirk.farin@gmail.com>
4
 *
5
 * This file is part of libheif.
6
 *
7
 * libheif is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Lesser General Public License as
9
 * published by the Free Software Foundation, either version 3 of
10
 * the License, or (at your option) any later version.
11
 *
12
 * libheif is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public License
18
 * along with libheif.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#ifndef LIBHEIF_FILELAYOUT_H
22
#define LIBHEIF_FILELAYOUT_H
23
24
#include "error.h"
25
#include "bitstream.h"
26
#include "box.h"
27
#include "mini.h"
28
29
#include <memory>
30
#include <vector>
31
32
class Box_moov;
33
34
35
class FileLayout
36
{
37
public:
38
  enum class WriteMode {
39
    Streaming, // 'mdat' data will be written to output immediately
40
    Floating,  // 'mdat' data will be held in memory until written
41
    TmpFile    // 'mdat' data will be written to temporary file and copied into final file
42
  };
43
44
  // Generate a file in WriteMode::Floating
45
  FileLayout();
46
47
  Error read(const std::shared_ptr<StreamReader>& stream, const heif_security_limits* limits);
48
49
  // For WriteMode::Streaming, writer cannot be null.
50
  void set_write_mode(WriteMode writeMode, const std::shared_ptr<StreamWriter>& writer = nullptr);
51
52
  // For WriteMode::Streaming, stream must be null.
53
  Error write(std::shared_ptr<StreamWriter>& stream);
54
55
56
  // --- access to boxes
57
58
0
  std::shared_ptr<Box_ftyp> get_ftyp_box() { return m_ftyp_box; }
59
60
0
  std::shared_ptr<Box_meta> get_meta_box() { return m_meta_box; }
61
62
0
  std::shared_ptr<Box_mini> get_mini_box() { return m_mini_box; }
63
64
0
  std::shared_ptr<Box_moov> get_moov_box() { return m_moov_box; }
65
66
private:
67
  WriteMode m_writeMode = WriteMode::Floating;
68
69
  const static uint64_t INVALID_FILE_SIZE = 0xFFFFFFFFFFFFFFFF;
70
71
  uint64_t m_file_size = INVALID_FILE_SIZE;
72
73
  // the first one is always 'ftyp'
74
  std::vector<std::shared_ptr<Box>> m_boxes; // TODO: do we need this ?
75
76
  std::shared_ptr<Box_ftyp> m_ftyp_box;
77
  std::shared_ptr<Box_meta> m_meta_box;
78
  std::shared_ptr<Box_mini> m_mini_box;
79
  std::shared_ptr<Box_moov> m_moov_box;
80
81
82
  uint64_t m_max_length = 0; // Length seen so far. It can grow over time.
83
84
  std::shared_ptr<StreamReader> m_stream_reader;
85
  std::shared_ptr<StreamWriter> m_stream_writer;
86
87
  static const uint64_t INITIAL_FTYP_REQUEST = 1024; // should be enough to read ftyp and next box header
88
  static const uint16_t MAXIMUM_BOX_HEADER_SIZE = 32;
89
};
90
91
#endif //LIBHEIF_FILELAYOUT_H