Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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  uint32_t mspf;
40 
41  if (avio_rl32(pb) != MKTAG('D', 'F', 'I', 'A')) {
42  av_log(s, AV_LOG_ERROR, "Invalid magic for DFA\n");
43  return AVERROR_INVALIDDATA;
44  }
45  avio_skip(pb, 2); // unused
46  frames = avio_rl16(pb);
47 
48  st = avformat_new_stream(s, NULL);
49  if (!st)
50  return AVERROR(ENOMEM);
51 
54  st->codec->width = avio_rl16(pb);
55  st->codec->height = avio_rl16(pb);
56  mspf = avio_rl32(pb);
57  if (!mspf) {
58  av_log(s, AV_LOG_WARNING, "Zero FPS reported, defaulting to 10\n");
59  mspf = 100;
60  }
61  avpriv_set_pts_info(st, 24, mspf, 1000);
62  avio_skip(pb, 128 - 16); // padding
63  st->duration = frames;
64 
65  return 0;
66 }
67 
69 {
70  AVIOContext *pb = s->pb;
71  uint32_t frame_size;
72  int ret, first = 1;
73 
74  if (pb->eof_reached)
75  return AVERROR_EOF;
76 
77  if (av_get_packet(pb, pkt, 12) != 12)
78  return AVERROR(EIO);
79  while (!pb->eof_reached) {
80  if (!first) {
81  ret = av_append_packet(pb, pkt, 12);
82  if (ret < 0) {
83  av_free_packet(pkt);
84  return ret;
85  }
86  } else
87  first = 0;
88  frame_size = AV_RL32(pkt->data + pkt->size - 8);
89  if (frame_size > INT_MAX - 4) {
90  av_log(s, AV_LOG_ERROR, "Too large chunk size: %d\n", frame_size);
91  return AVERROR(EIO);
92  }
93  if (AV_RL32(pkt->data + pkt->size - 12) == MKTAG('E', 'O', 'F', 'R')) {
94  if (frame_size) {
95  av_log(s, AV_LOG_WARNING, "skipping %d bytes of end-of-frame marker chunk\n",
96  frame_size);
97  avio_skip(pb, frame_size);
98  }
99  return 0;
100  }
101  ret = av_append_packet(pb, pkt, frame_size);
102  if (ret < 0) {
103  av_free_packet(pkt);
104  return ret;
105  }
106  }
107 
108  return 0;
109 }
110 
112  .name = "dfa",
113  .long_name = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
114  .read_probe = dfa_probe,
115  .read_header = dfa_read_header,
116  .read_packet = dfa_read_packet,
117  .flags = AVFMT_GENERIC_INDEX,
118 };
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:242
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
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:3208
int size
Definition: avcodec.h:974
static int dfa_read_header(AVFormatContext *s)
Definition: dfa.c:34
Format I/O context.
Definition: avformat.h:871
uint8_t * data
Definition: avcodec.h:973
#define AVERROR_EOF
End of file.
Definition: error.h:51
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:118
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:128
static const uint8_t frame_size[4]
Definition: g723_1_data.h:47
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:558
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:142
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:148
AVStream * avformat_new_stream(AVFormatContext *s, AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2662
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:702
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:391
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:390
int width
picture width / height.
Definition: avcodec.h:1217
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:210
#define AV_RL32
Definition: intreadwrite.h:146
Stream structure.
Definition: avformat.h:683
NULL
Definition: eval.c:55
enum AVMediaType codec_type
Definition: avcodec.h:1062
enum AVCodecID codec_id
Definition: avcodec.h:1065
AVIOContext * pb
I/O context.
Definition: avformat.h:913
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:408
AVInputFormat ff_dfa_demuxer
Definition: dfa.c:111
This structure contains the data a format has to probe a file.
Definition: avformat.h:388
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:734
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:395
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:542
Main libavformat public API header.
static int dfa_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: dfa.c:68
static int dfa_probe(AVProbeData *p)
Definition: dfa.c:26
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:516
#define MKTAG(a, b, c, d)
Definition: common.h:238
This structure stores compressed data.
Definition: avcodec.h:950