Skip to content

Commit 68f3152

Browse files
authored
Address FFmpeg/devel review comments
* Rename variables and functions * Fix rational initialization * Fix memory leaks when heap allocation fails * Replaced `unsigned long` with `uint<xx>_t` * Make `asset_locator_map` a stack object * `imf_close()` is now called automatically * Miscellaneous code path optimizations * Code style improved
1 parent a7761a4 commit 68f3152

File tree

7 files changed

+795
-606
lines changed

7 files changed

+795
-606
lines changed

.clang-format

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ AlignAfterOpenBracket: false
44
AlignConsecutiveAssignments: false
55
AlignConsecutiveDeclarations: false
66
AlignOperands: false
7-
AlignTrailingComments: false
7+
AlignTrailingComments: true
88
AllowAllParametersOfDeclarationOnNextLine: true
99
AllowShortBlocksOnASingleLine: false
1010
AllowShortCaseLabelsOnASingleLine: false
@@ -22,6 +22,7 @@ BraceWrapping:
2222
AfterFunction: true
2323
BreakBeforeTernaryOperators: false
2424
BreakConstructorInitializersBeforeComma: true
25+
BreakStringLiterals: true
2526
ColumnLimit: 0
2627
CommentPragmas: '^ IWYU pragma:'
2728
ContinuationIndentWidth: 4

imf-prepare-patches.sh

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
set -e
44

5-
PATCH_VERSION="5"
5+
PATCH_VERSION="9"
66

77
PATCH_NAME="avformat/imf"
88

@@ -11,14 +11,12 @@ PATCH_BRANCH="rd/patches"
1111

1212
PATCHES_DIR="build/patches"
1313

14-
PATCHES_IMF_HEADERS="libavformat/imf.h"
15-
PATCHES_IMF_DEC="libavformat/imfdec.c"
16-
PATCHES_IMF_CPL="libavformat/imf_cpl.c"
17-
PATCHES_IMF_TESTS="libavformat/tests/imf.c"
18-
PATCHES_MXF="libavformat/mxf.h libavformat/mxfdec.c"
14+
PATCHES_SRC="libavformat/imf.h libavformat/imf_cpl.c libavformat/imfdec.c"
1915
PATCHES_MISC="MAINTAINERS configure doc/demuxers.texi libavformat/Makefile libavformat/allformats.c"
16+
PATCHES_MAKEFILE="libavformat/Makefile"
17+
PATCHES_TESTS="libavformat/tests/imf.c"
2018

21-
PATCHES_ALL="$PATCHES_IMF_HEADERS $PATCHES_IMF_DEC $PATCHES_IMF_CPL $PATCHES_IMF_TESTS $PATCHES_MXF $PATCHES_MISC"
19+
PATCHES_ALL="$PATCHES_SRC $PATCHES_MAKEFILE $PATCHES_MISC $PATCHES_TESTS"
2220

2321
git fetch --all
2422

@@ -30,9 +28,8 @@ git rebase $BASE_BRANCH
3028

3129
git reset $BASE_BRANCH
3230

33-
AUGMENTED=" * POSSIBILITY OF SUCH DAMAGE.\n\
34-
*\n\
35-
* This file is part of FFmpeg.\n\
31+
# update copyright header
32+
GPLCC=" * This file is part of FFmpeg.\n\
3633
*\n\
3734
* FFmpeg is free software; you can redistribute it and\/or\n\
3835
* modify it under the terms of the GNU Lesser General Public\n\
@@ -46,14 +43,24 @@ AUGMENTED=" * POSSIBILITY OF SUCH DAMAGE.\n\
4643
*\n\
4744
* You should have received a copy of the GNU Lesser General Public\n\
4845
* License along with FFmpeg; if not, write to the Free Software\n\
49-
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA"
46+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n\
47+
*\\/\n\
48+
\n\
49+
\\/*"
50+
51+
sed -i "s/^ \* This file is part of FFmpeg\./$GPLCC/" $PATCHES_SRC $PATCHES_TESTS
5052

51-
sed -i "/^ \* This file is part of FFmpeg\./,+1 d" $PATCHES_IMF_HEADERS $PATCHES_IMF_DEC $PATCHES_IMF_CPL $PATCHES_IMF_TESTS
52-
sed -i "s/^ \* POSSIBILITY OF SUCH DAMAGE\./$AUGMENTED/" $PATCHES_IMF_HEADERS $PATCHES_IMF_DEC $PATCHES_IMF_CPL $PATCHES_IMF_TESTS
53+
# remove MXF documentation
54+
sed -i "/^@section mxf/,+12d" doc/demuxers.texi
5355

54-
git add -- $PATCHES_ALL
56+
# remove clang formatting commands
57+
sed -i "/^\\/\\/ clang-format/d" $PATCHES_SRC $PATCHES_TESTS
5558

56-
git commit -m "${PATCH_NAME}: Headers" -- $PATCHES_IMF_HEADERS
59+
# remove tests from Makefile
60+
sed -i "/^TESTPROGS-\$(CONFIG_IMF_DEMUXER)/d" $PATCHES_MAKEFILE
61+
62+
git add -- $PATCHES_SRC $PATCHES_MISC $PATCHES_MAKEFILE
63+
git commit -m "${PATCH_NAME}: Demuxer" -- $PATCHES_SRC $PATCHES_MISC $PATCHES_MAKEFILE
5764
git notes add -m "The IMF demuxer accepts as input an IMF CPL. The assets referenced by the CPL can be
5865
contained in multiple deliveries, each defined by an ASSETMAP file:
5966
@@ -73,32 +80,16 @@ The location of the Track Files referenced by the Composition Playlist is stored
7380
in one or more XML documents called Asset Maps. More details at https://www.imfug.com/explainer.
7481
The IMF standard was first introduced in 2013 and is managed by the SMPTE.
7582
76-
Header and build files.
77-
7883
CHANGE NOTES:
7984
80-
- fixed patchwork warnings
81-
- updated patch notes
82-
- added LGPL license
83-
- removed imf_internal.h
84-
- Improve error handling, including removing exit()
85-
- Fix code style
86-
- Allow custom I/O for all files (following DASH and HLS template)
87-
- replace realloc with av_realloc_f to fix leaks"
88-
89-
# git commit -m "[IMF demuxer] MCA improvements to MXF decoder" -- $PATCHES_MXF
90-
# git notes add -m "Add support for SMPTE ST 377-4 (Multichannel Audio Labeling -- MCA) \
91-
# to the MXF decoder. MCA allows arbitrary audio channel configurations \
92-
# in MXF files."
93-
94-
git commit -m "${PATCH_NAME}: CPL processor" -- $PATCHES_IMF_CPL
95-
git notes add -m "Implements IMF Composition Playlist (CPL) parsing."
96-
97-
git commit -m "${PATCH_NAME}: Demuxer implementation" -- $PATCHES_IMF_DEC
98-
git notes add -m "Implements the IMF demuxer."
85+
- fix leaks when head allocation fails
86+
"
9987

100-
git commit -m "${PATCH_NAME}: Tests and build files" -- $PATCHES_IMF_TESTS $PATCHES_MISC
101-
git notes add -m "Tests and build files for the IMF demuxer."
88+
# add tests back to the Makefile
89+
sed -i "/^TESTPROGS-\$(CONFIG_SRTP)/a TESTPROGS-\$(CONFIG_IMF_DEMUXER) += imf" $PATCHES_MAKEFILE
90+
git add -- $PATCHES_TESTS $PATCHES_MAKEFILE
91+
git commit -m "${PATCH_NAME}: Tests" -- $PATCHES_TESTS $PATCHES_MAKEFILE
92+
git notes add -m "Tests for the IMF demuxer."
10293

10394
mkdir -p $PATCHES_DIR
10495

imf-unit-tests.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#!/bin/sh
22

33
valgrind --leak-check=full ./ffmpeg -y -i http://ffmpeg-imf-samples-public.s3.us-west-1.amazonaws.com/callout_51_l_r_c_lfe_ls_rs.mxf -f wav /dev/null
4-
valgrind --leak-check=full ./ffmpeg -y -i http://ffmpeg-imf-samples-public.s3-website-us-west-1.amazonaws.com/countdown/CPL_f5095caa-f204-4e1c-8a84-7af48c7ae16b.xml -f mp4 /dev/null
4+
5+
valgrind --leak-check=full ./ffmpeg -y \
6+
-assetmaps http://ffmpeg-imf-samples-public.s3.us-west-1.amazonaws.com/countdown-qc/ASSETMAP.xml,http://ffmpeg-imf-samples-public.s3.us-west-1.amazonaws.com/countdown/ASSETMAP.xml \
7+
-i http://ffmpeg-imf-samples-public.s3-website-us-west-1.amazonaws.com/countdown/CPL_f5095caa-f204-4e1c-8a84-7af48c7ae16b.xml \
8+
-f mp4 /dev/null
59

610
make libavformat/tests/imf
711
valgrind --leak-check=full libavformat/tests/imf

libavformat/imf.h

Lines changed: 73 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626
*/
2727

2828
/**
29-
* Public header file for the processing of Interoperable Master Format (IMF) packages.
30-
*
29+
* Public header file for the processing of Interoperable Master Format (IMF)
30+
* packages.
31+
*
3132
* @author Pierre-Anthony Lemieux
3233
* @author Valentin Noel
3334
* @file
@@ -42,142 +43,148 @@
4243
#include "libavutil/rational.h"
4344
#include <libxml/tree.h>
4445

45-
#define IMF_UUID_FORMAT "urn:uuid:%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
46+
#define FF_UUID_FORMAT \
47+
"urn:uuid:%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-" \
48+
"%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
4649

4750
/**
4851
* UUID as defined in IETF RFC 422
4952
*/
50-
typedef uint8_t UUID[16];
53+
typedef uint8_t FFUUID[16];
5154

5255
/**
5356
* IMF Composition Playlist Base Resource
5457
*/
55-
typedef struct IMFBaseResource {
56-
AVRational edit_rate; /**< BaseResourceType/EditRate */
57-
unsigned long entry_point; /**< BaseResourceType/EntryPoint */
58-
unsigned long duration; /**< BaseResourceType/Duration */
59-
unsigned long repeat_count; /**< BaseResourceType/RepeatCount */
60-
} IMFBaseResource;
58+
typedef struct FFIMFBaseResource {
59+
AVRational edit_rate; /**< BaseResourceType/EditRate */
60+
uint32_t entry_point; /**< BaseResourceType/EntryPoint */
61+
uint32_t duration; /**< BaseResourceType/Duration */
62+
uint32_t repeat_count; /**< BaseResourceType/RepeatCount */
63+
} FFIMFBaseResource;
6164

6265
/**
6366
* IMF Composition Playlist Track File Resource
6467
*/
65-
typedef struct IMFTrackFileResource {
66-
IMFBaseResource base;
67-
UUID track_file_uuid; /**< TrackFileResourceType/TrackFileId */
68-
} IMFTrackFileResource;
68+
typedef struct FFIMFTrackFileResource {
69+
FFIMFBaseResource base;
70+
FFUUID track_file_uuid; /**< TrackFileResourceType/TrackFileId */
71+
} FFIMFTrackFileResource;
6972

7073
/**
7174
* IMF Marker
7275
*/
73-
typedef struct IMFMarker {
76+
typedef struct FFIMFMarker {
7477
xmlChar *label_utf8; /**< Marker/Label */
7578
xmlChar *scope_utf8; /**< Marker/Label/\@scope */
76-
unsigned long offset; /**< Marker/Offset */
77-
} IMFMarker;
79+
uint32_t offset; /**< Marker/Offset */
80+
} FFIMFMarker;
7881

7982
/**
8083
* IMF Composition Playlist Marker Resource
8184
*/
82-
typedef struct IMFMarkerResource {
83-
IMFBaseResource base;
84-
unsigned long marker_count; /**< Number of Marker elements */
85-
IMFMarker *markers; /**< Marker elements */
86-
} IMFMarkerResource;
85+
typedef struct FFIMFMarkerResource {
86+
FFIMFBaseResource base;
87+
uint32_t marker_count; /**< Number of Marker elements */
88+
FFIMFMarker *markers; /**< Marker elements */
89+
} FFIMFMarkerResource;
8790

8891
/**
8992
* IMF Composition Playlist Virtual Track
9093
*/
91-
typedef struct IMFBaseVirtualTrack {
92-
UUID id_uuid; /**< TrackId associated with the Virtual Track */
93-
} IMFBaseVirtualTrack;
94+
typedef struct FFIMFBaseVirtualTrack {
95+
FFUUID id_uuid; /**< TrackId associated with the Virtual Track */
96+
} FFIMFBaseVirtualTrack;
9497

9598
/**
9699
* IMF Composition Playlist Virtual Track that consists of Track File Resources
97100
*/
98-
typedef struct IMFTrackFileVirtualTrack {
99-
IMFBaseVirtualTrack base;
100-
unsigned long resource_count; /**< Number of Resource elements present in the Virtual Track */
101-
IMFTrackFileResource *resources; /**< Resource elements of the Virtual Track */
102-
} IMFTrackFileVirtualTrack;
101+
typedef struct FFIMFTrackFileVirtualTrack {
102+
FFIMFBaseVirtualTrack base;
103+
uint32_t resource_count; /**< Number of Resource elements present in the Virtual Track */
104+
FFIMFTrackFileResource *resources; /**< Resource elements of the Virtual Track */
105+
uint32_t resources_alloc_sz; /**< Size of the resources buffer */
106+
} FFIMFTrackFileVirtualTrack;
103107

104108
/**
105109
* IMF Composition Playlist Virtual Track that consists of Marker Resources
106110
*/
107-
typedef struct IMFMarkerVirtualTrack {
108-
IMFBaseVirtualTrack base;
109-
unsigned long resource_count; /**< Number of Resource elements present in the Virtual Track */
110-
IMFMarkerResource *resources; /**< Resource elements of the Virtual Track */
111-
} IMFMarkerVirtualTrack;
111+
typedef struct FFIMFMarkerVirtualTrack {
112+
FFIMFBaseVirtualTrack base;
113+
uint32_t resource_count; /**< Number of Resource elements present in the Virtual Track */
114+
FFIMFMarkerResource *resources; /**< Resource elements of the Virtual Track */
115+
} FFIMFMarkerVirtualTrack;
112116

113117
/**
114118
* IMF Composition Playlist
115119
*/
116-
typedef struct IMFCPL {
117-
UUID id_uuid; /**< CompositionPlaylist/Id element */
118-
xmlChar *content_title_utf8; /**< CompositionPlaylist/ContentTitle element */
119-
AVRational edit_rate; /**< CompositionPlaylist/EditRate element */
120-
IMFMarkerVirtualTrack *main_markers_track; /**< Main Marker Virtual Track */
121-
IMFTrackFileVirtualTrack *main_image_2d_track; /**< Main Image Virtual Track */
122-
unsigned long main_audio_track_count; /**< Number of Main Audio Virtual Tracks */
123-
IMFTrackFileVirtualTrack *main_audio_tracks; /**< Main Audio Virtual Tracks */
124-
} IMFCPL;
120+
typedef struct FFIMFCPL {
121+
FFUUID id_uuid; /**< CompositionPlaylist/Id element */
122+
xmlChar *content_title_utf8; /**< CompositionPlaylist/ContentTitle element */
123+
AVRational edit_rate; /**< CompositionPlaylist/EditRate element */
124+
FFIMFMarkerVirtualTrack *main_markers_track; /**< Main Marker Virtual Track */
125+
FFIMFTrackFileVirtualTrack *main_image_2d_track; /**< Main Image Virtual Track */
126+
uint32_t main_audio_track_count; /**< Number of Main Audio Virtual Tracks */
127+
FFIMFTrackFileVirtualTrack *main_audio_tracks; /**< Main Audio Virtual Tracks */
128+
} FFIMFCPL;
125129

126130
/**
127-
* Parse an IMF CompositionPlaylist element into the IMFCPL data structure.
131+
* Parse an IMF CompositionPlaylist element into the FFIMFCPL data structure.
128132
* @param[in] doc An XML document from which the CPL is read.
129-
* @param[out] cpl Pointer to a memory area (allocated by the client), where the function writes a pointer to the newly constructed
130-
* IMFCPL structure (or NULL if the CPL could not be parsed). The client is responsible for freeing the IMFCPL structure using
131-
* imf_cpl_free().
133+
* @param[out] cpl Pointer to a memory area (allocated by the client), where the
134+
* function writes a pointer to the newly constructed FFIMFCPL structure (or
135+
* NULL if the CPL could not be parsed). The client is responsible for freeing
136+
* the FFIMFCPL structure using ff_imf_cpl_free().
132137
* @return A non-zero value in case of an error.
133138
*/
134-
int parse_imf_cpl_from_xml_dom(xmlDocPtr doc, IMFCPL **cpl);
139+
int ff_parse_imf_cpl_from_xml_dom(xmlDocPtr doc, FFIMFCPL **cpl);
135140

136141
/**
137-
* Parse an IMF Composition Playlist document into the IMFCPL data structure.
142+
* Parse an IMF Composition Playlist document into the FFIMFCPL data structure.
138143
* @param[in] in The context from which the CPL is read.
139-
* @param[out] cpl Pointer to a memory area (allocated by the client), where the function writes a pointer to the newly constructed
140-
* IMFCPL structure (or NULL if the CPL could not be parsed). The client is responsible for freeing the IMFCPL structure using
141-
* imf_cpl_free().
144+
* @param[out] cpl Pointer to a memory area (allocated by the client), where the
145+
* function writes a pointer to the newly constructed FFIMFCPL structure (or
146+
* NULL if the CPL could not be parsed). The client is responsible for freeing
147+
* the FFIMFCPL structure using ff_imf_cpl_free().
142148
* @return A non-zero value in case of an error.
143149
*/
144-
int parse_imf_cpl(AVIOContext *in, IMFCPL **cpl);
150+
int ff_parse_imf_cpl(AVIOContext *in, FFIMFCPL **cpl);
145151

146152
/**
147-
* Allocates and initializes an IMFCPL data structure.
148-
* @return A pointer to the newly constructed IMFCPL structure (or NULL if the structure could not be constructed). The client is
149-
* responsible for freeing the IMFCPL structure using imf_cpl_free().
153+
* Allocates and initializes an FFIMFCPL data structure.
154+
* @return A pointer to the newly constructed FFIMFCPL structure (or NULL if the
155+
* structure could not be constructed). The client is responsible for freeing
156+
* the FFIMFCPL structure using ff_imf_cpl_free().
150157
*/
151-
IMFCPL *imf_cpl_alloc(void);
158+
FFIMFCPL *ff_imf_cpl_alloc(void);
152159

153160
/**
154-
* Deletes an IMFCPL data structure previously instantiated with imf_cpl_alloc().
155-
* @param[in] cpl The IMFCPL structure to delete.
161+
* Deletes an FFIMFCPL data structure previously instantiated with ff_imf_cpl_alloc().
162+
* @param[in] cpl The FFIMFCPL structure to delete.
156163
*/
157-
void imf_cpl_free(IMFCPL *cpl);
164+
void ff_imf_cpl_free(FFIMFCPL *cpl);
158165

159166
/**
160-
* Reads an unsigned long from an XML element
167+
* Reads an unsigned 32-bit integer from an XML element
161168
* @return 0 on success, < 0 AVERROR code on error.
162169
*/
163-
int imf_xml_read_ulong(xmlNodePtr element, unsigned long *number);
170+
int ff_xml_read_uint32(xmlNodePtr element, uint32_t *number);
164171

165172
/**
166173
* Reads an AVRational from an XML element
167174
* @return 0 on success, < 0 AVERROR code on error.
168175
*/
169-
int imf_xml_read_rational(xmlNodePtr element, AVRational *rational);
176+
int ff_xml_read_rational(xmlNodePtr element, AVRational *rational);
170177

171178
/**
172179
* Reads a UUID from an XML element
173180
* @return 0 on success, < 0 AVERROR code on error.
174181
*/
175-
int imf_xml_read_UUID(xmlNodePtr element, uint8_t uuid[16]);
182+
int ff_xml_read_uuid(xmlNodePtr element, uint8_t uuid[16]);
176183

177184
/**
178185
* Returns the first child element with the specified local name
179186
* @return A pointer to the child element, or NULL if no such child element exists.
180187
*/
181-
xmlNodePtr imf_xml_get_child_element_by_name(xmlNodePtr parent, const char *name_utf8);
188+
xmlNodePtr ff_xml_get_child_element_by_name(xmlNodePtr parent, const char *name_utf8);
182189

183190
#endif

0 commit comments

Comments
 (0)