ChapterForge
Loading...
Searching...
No Matches
mp4_atoms.hpp
Go to the documentation of this file.
1//
2// mp4_atoms.hpp
3// ChapterForge
4//
5// Created by Till Toenshoff on 12/9/25.
6// Copyright © 2025 Till Toenshoff. All rights reserved.
7//
8
9#pragma once
10#include <cstdint>
11#include <fstream>
12#include <memory>
13#include <stdexcept>
14#include <string>
15#include <vector>
16
17#include "fourcc_utils.hpp"
18
19// Forward declaration.
20class Atom;
21
22using AtomPtr = std::unique_ptr<Atom>;
23
24class Atom {
25 public:
26 uint32_t type = 0; // FourCC
27 std::vector<uint8_t> payload; // Raw payload (before children)
28 std::vector<AtomPtr> children; // Nested boxes
29
30 uint32_t box_size = 0; // Computed via fix_size_recursive()
31
32 Atom() = default;
33 explicit Atom(uint32_t t) : type(t) {}
34 explicit Atom(const char t[4]) : type(fourcc(t)) {}
35
36 // Factory.
37 static AtomPtr create(const char t[4]);
38 static AtomPtr create(uint32_t t);
39 static AtomPtr create(const std::string &t);
40
41 // Add child atom.
42 void add(AtomPtr child);
43
44 // Recursive search for atoms of given type.
45 std::vector<Atom *> find(const std::string &t);
46
47 // Recursive size computation.
49
50 // Return size (must call fix_size_recursive first)
51 uint32_t size() const;
52
53 // Write atom to file.
54 void write(std::ofstream &out) const;
55};
56
57// ------------- Helper write functions ---------------------------------------
58
59inline void write_u8(std::vector<uint8_t> &p, uint8_t v) { p.push_back(v); }
60
61inline void write_u16(std::vector<uint8_t> &p, uint16_t v) {
62 p.push_back((v >> 8) & 0xFF);
63 p.push_back(v & 0xFF);
64}
65
66inline void write_u24(std::vector<uint8_t> &p, uint32_t v) {
67 p.push_back((v >> 16) & 0xFF);
68 p.push_back((v >> 8) & 0xFF);
69 p.push_back(v & 0xFF);
70}
71
72inline void write_u32(std::vector<uint8_t> &p, uint32_t v) {
73 p.push_back((v >> 24) & 0xFF);
74 p.push_back((v >> 16) & 0xFF);
75 p.push_back((v >> 8) & 0xFF);
76 p.push_back(v & 0xFF);
77}
78
79inline void write_u64(std::vector<uint8_t> &p, uint64_t v) {
80 p.push_back((v >> 56) & 0xFF);
81 p.push_back((v >> 48) & 0xFF);
82 p.push_back((v >> 40) & 0xFF);
83 p.push_back((v >> 32) & 0xFF);
84 p.push_back((v >> 24) & 0xFF);
85 p.push_back((v >> 16) & 0xFF);
86 p.push_back((v >> 8) & 0xFF);
87 p.push_back(v & 0xFF);
88}
89
90inline void write_fixed16_16(std::vector<uint8_t> &out, float f) {
91 uint32_t v = static_cast<uint32_t>(f * 65536.0f);
92 write_u32(out, v);
93}
Definition mp4_atoms.hpp:24
void write(std::ofstream &out) const
Atom()=default
Atom(uint32_t t)
Definition mp4_atoms.hpp:33
void add(AtomPtr child)
uint32_t box_size
Definition mp4_atoms.hpp:30
uint32_t size() const
std::vector< Atom * > find(const std::string &t)
static AtomPtr create(const std::string &t)
static AtomPtr create(uint32_t t)
Atom(const char t[4])
Definition mp4_atoms.hpp:34
static AtomPtr create(const char t[4])
std::vector< uint8_t > payload
Definition mp4_atoms.hpp:27
std::vector< AtomPtr > children
Definition mp4_atoms.hpp:28
uint32_t type
Definition mp4_atoms.hpp:26
void fix_size_recursive()
constexpr uint32_t fourcc(const char a, const char b, const char c, const char d)
Definition fourcc_utils.hpp:16
void write_u8(std::vector< uint8_t > &p, uint8_t v)
Definition mp4_atoms.hpp:59
void write_u24(std::vector< uint8_t > &p, uint32_t v)
Definition mp4_atoms.hpp:66
void write_u64(std::vector< uint8_t > &p, uint64_t v)
Definition mp4_atoms.hpp:79
void write_u32(std::vector< uint8_t > &p, uint32_t v)
Definition mp4_atoms.hpp:72
void write_u16(std::vector< uint8_t > &p, uint16_t v)
Definition mp4_atoms.hpp:61
void write_fixed16_16(std::vector< uint8_t > &out, float f)
Definition mp4_atoms.hpp:90
std::unique_ptr< Atom > AtomPtr
Definition mp4_atoms.hpp:22