libavformat/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 "libavutil/intreadwrite.h"
23 #include "avformat.h"
24 #include "internal.h"
25 
26 static int dfa_probe(AVProbeData *p)
27 {
28  if (p->buf_size < 4 || AV_RL32(p->buf) != MKTAG('D', 'F', 'I', 'A'))
29  return 0;
30 
31  return AVPROBE_SCORE_MAX;
32 }
33 
35 {
36  AVIOContext *pb = s->pb;
37  AVStream *st;
38  int frames;
39  int version;
40  uint32_t mspf;
41 
42  if (avio_rl32(pb) != MKTAG('D', 'F', 'I', 'A')) {
43  av_log(s, AV_LOG_ERROR, "Invalid magic for DFA\n");
44  return AVERROR_INVALIDDATA;
45  }
46 
47  version = avio_rl16(pb);
48  frames = avio_rl16(pb);
49 
50  st = avformat_new_stream(s, NULL);
51  if (!st)
52  return AVERROR(ENOMEM);
53 
56  st->codec->width = avio_rl16(pb);
57  st->codec->height = avio_rl16(pb);
58  mspf = avio_rl32(pb);
59  if (!mspf) {
60  av_log(s, AV_LOG_WARNING, "Zero FPS reported, defaulting to 10\n");
61  mspf = 100;
62  }
63  avpriv_set_pts_info(st, 24, mspf, 1000);
64  avio_skip(pb, 128 - 16); // padding
65  st->duration = frames;
66 
67  st->codec->extradata = av_malloc(2);
68  st->codec->extradata_size = 2;
69  AV_WL16(st->codec->extradata, version);
70  if (version == 0x100)
71  st->sample_aspect_ratio = (AVRational){2, 1};
72 
73  return 0;
74 }
75 
77 {
78  AVIOContext *pb = s->pb;
79  uint32_t frame_size;
80  int ret, first = 1;
81 
82  if (pb->eof_reached)
83  return AVERROR_EOF;
84 
85  if (av_get_packet(pb, pkt, 12) != 12)
86  return AVERROR(EIO);
87  while (!pb->eof_reached) {
88  if (!first) {
89  ret = av_append_packet(pb, pkt, 12);
90  if (ret < 0) {
91  av_free_packet(pkt);
92  return ret;
93  }
94  } else
95  first = 0;
96  frame_size = AV_RL32(pkt->data + pkt->size - 8);
97  if (frame_size > INT_MAX - 4) {
98  av_log(s, AV_LOG_ERROR, "Too large chunk size: %d\n", frame_size);
99  return AVERROR(EIO);
100  }
101  if (AV_RL32(pkt->data + pkt->size - 12) == MKTAG('E', 'O', 'F', 'R')) {
102  if (frame_size) {
103  av_log(s, AV_LOG_WARNING, "skipping %d bytes of end-of-frame marker chunk\n",
104  frame_size);
105  avio_skip(pb, frame_size);
106  }
107  return 0;
108  }
109  ret = av_append_packet(pb, pkt, frame_size);
110  if (ret < 0) {
111  av_free_packet(pkt);
112  return ret;
113  }
114  }
115 
116  return 0;
117 }
118 
120  .name = "dfa",
121  .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
122  .read_probe = dfa_probe,
123  .read_header = dfa_read_header,
124  .read_packet = dfa_read_packet,
125  .flags = AVFMT_GENERIC_INDEX,
126 };
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:242
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.
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:154
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:709
static int dfa_read_header(AVFormatContext *s)
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:256
int version
Definition: avisynth_c.h:666
Format I/O context.
Definition: avformat.h:944
static AVPacket pkt
Definition: demuxing.c:56
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But first
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
uint8_t * data
#define AVERROR_EOF
End of file.
Definition: error.h:55
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.
int av_append_packet(AVIOContext *s, AVPacket *pkt, int size)
Read data and append it to the current content of the AVPacket.
static const uint8_t frame_size[4]
Definition: g723_1_data.h:58
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:579
#define AV_WL16(p, darg)
Definition: intreadwrite.h:250
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:337
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:336
struct AVRational AVRational
rational number numerator/denominator
ret
Definition: avfilter.c:821
int width
picture width / height.
#define AV_RL32
if it could not because there are no more frames
Stream structure.
Definition: avformat.h:643
NULL
Definition: eval.c:55
enum AVMediaType codec_type
enum AVCodecID codec_id
AVIOContext * pb
I/O context.
Definition: avformat.h:977
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:353
AVInputFormat ff_dfa_demuxer
This structure contains the data a format has to probe a file.
Definition: avformat.h:334
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:696
#define AVPROBE_SCORE_MAX
maximum score, half of that is used for file-extension-based detection
Definition: avformat.h:340
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:563
Main libavformat public API header.
static int dfa_read_packet(AVFormatContext *s, AVPacket *pkt)
static int dfa_probe(AVProbeData *p)
int eof_reached
true if eof reached
Definition: avio.h:96
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:461
#define MKTAG(a, b, c, d)
Definition: common.h:282
This structure stores compressed data.