FFmpeg
dfa.c
Go to the documentation of this file.
1 /*
2  * Chronomaster DFA Format Demuxer
3  * Copyright (c) 2011 Konstantin Shishkov
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg 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 GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <inttypes.h>
23 
24 #include "libavutil/intreadwrite.h"
25 #include "avformat.h"
26 #include "internal.h"
27 
28 static int dfa_probe(const AVProbeData *p)
29 {
30  if (p->buf_size < 4 || AV_RL32(p->buf) != MKTAG('D', 'F', 'I', 'A'))
31  return 0;
32 
33  if (AV_RL32(p->buf + 16) != 0x80)
34  return AVPROBE_SCORE_MAX / 4;
35 
36  return AVPROBE_SCORE_MAX;
37 }
38 
40 {
41  AVIOContext *pb = s->pb;
42  AVStream *st;
43  int frames;
44  int version;
45  uint32_t mspf;
46 
47  if (avio_rl32(pb) != MKTAG('D', 'F', 'I', 'A')) {
48  av_log(s, AV_LOG_ERROR, "Invalid magic for DFA\n");
49  return AVERROR_INVALIDDATA;
50  }
51 
52  version = avio_rl16(pb);
53  frames = avio_rl16(pb);
54 
55  st = avformat_new_stream(s, NULL);
56  if (!st)
57  return AVERROR(ENOMEM);
58 
61  st->codecpar->width = avio_rl16(pb);
62  st->codecpar->height = avio_rl16(pb);
63  mspf = avio_rl32(pb);
64  if (!mspf) {
65  av_log(s, AV_LOG_WARNING, "Zero FPS reported, defaulting to 10\n");
66  mspf = 100;
67  }
68  avpriv_set_pts_info(st, 24, mspf, 1000);
69  avio_skip(pb, 128 - 16); // padding
70  st->duration = frames;
71 
72  if (ff_alloc_extradata(st->codecpar, 2))
73  return AVERROR(ENOMEM);
75  if (version == 0x100)
76  st->sample_aspect_ratio = (AVRational){2, 1};
77 
78  return 0;
79 }
80 
82 {
83  AVIOContext *pb = s->pb;
84  uint32_t frame_size;
85  int ret, first = 1;
86 
87  if (avio_feof(pb))
88  return AVERROR_EOF;
89 
90  if (av_get_packet(pb, pkt, 12) != 12)
91  return AVERROR(EIO);
92  while (!avio_feof(pb)) {
93  if (!first) {
94  ret = av_append_packet(pb, pkt, 12);
95  if (ret < 0) {
97  return ret;
98  }
99  } else
100  first = 0;
101  frame_size = AV_RL32(pkt->data + pkt->size - 8);
102  if (frame_size > INT_MAX - 4) {
103  av_log(s, AV_LOG_ERROR, "Too large chunk size: %"PRIu32"\n", frame_size);
105  return AVERROR(EIO);
106  }
107  if (AV_RL32(pkt->data + pkt->size - 12) == MKTAG('E', 'O', 'F', 'R')) {
108  if (frame_size) {
110  "skipping %"PRIu32" bytes of end-of-frame marker chunk\n",
111  frame_size);
112  avio_skip(pb, frame_size);
113  }
114  return 0;
115  }
117  if (ret < 0) {
119  return ret;
120  }
121  }
122 
123  return 0;
124 }
125 
127  .name = "dfa",
128  .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
129  .read_probe = dfa_probe,
130  .read_header = dfa_read_header,
131  .read_packet = dfa_read_packet,
132  .flags = AVFMT_GENERIC_INDEX,
133 };
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:599
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3971
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4480
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3953
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:55
MKTAG
#define MKTAG(a, b, c, d)
Definition: common.h:366
AVPacket::data
uint8_t * data
Definition: avcodec.h:1477
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:449
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:458
frames
if it could not because there are no more frames
Definition: filter_design.txt:266
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:468
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:919
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:753
first
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But first
Definition: rate_distortion.txt:12
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVInputFormat
Definition: avformat.h:640
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:645
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:448
frame_size
int frame_size
Definition: mxfenc.c:2215
AVCodecParameters::width
int width
Video only.
Definition: avcodec.h:4023
version
int version
Definition: avisynth_c.h:858
dfa_read_packet
static int dfa_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: dfa.c:81
AVFormatContext
Format I/O context.
Definition: avformat.h:1342
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1017
NULL
#define NULL
Definition: coverity.c:32
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:446
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:769
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
AVPacket::size
int size
Definition: avcodec.h:1478
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:188
ff_dfa_demuxer
AVInputFormat ff_dfa_demuxer
Definition: dfa.c:126
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4910
AVStream::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:932
AV_WL16
#define AV_WL16(p, v)
Definition: intreadwrite.h:412
AVCodecParameters::height
int height
Definition: avcodec.h:4024
dfa_probe
static int dfa_probe(const AVProbeData *p)
Definition: dfa.c:28
dfa_read_header
static int dfa_read_header(AVFormatContext *s)
Definition: dfa.c:39
av_get_packet
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:313
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:870
av_append_packet
int av_append_packet(AVIOContext *s, AVPacket *pkt, int size)
Read data and append it to the current content of the AVPacket.
Definition: utils.c:323
avformat.h
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:88
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:331
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3957
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1454
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
AV_CODEC_ID_DFA
@ AV_CODEC_ID_DFA
Definition: avcodec.h:368
ff_alloc_extradata
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0.
Definition: utils.c:3309
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:358