FFmpeg
mxg.c
Go to the documentation of this file.
1 /*
2  * MxPEG clip file demuxer
3  * Copyright (c) 2010 Anatoly Nenashev
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 
23 #include "libavutil/internal.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/mem.h"
26 #include "libavcodec/mjpeg.h"
27 #include "avformat.h"
28 #include "demux.h"
29 #include "internal.h"
30 #include "avio.h"
31 
32 #define DEFAULT_PACKET_SIZE 1024
33 #define OVERREAD_SIZE 3
34 
35 typedef struct MXGContext {
36  uint8_t *buffer;
37  uint8_t *buffer_ptr;
38  uint8_t *soi_ptr;
39  unsigned int buffer_size;
41  unsigned int cache_size;
42 } MXGContext;
43 
45 {
47  MXGContext *mxg = s->priv_data;
48 
49  /* video parameters will be extracted from the compressed bitstream */
51  if (!video_st)
52  return AVERROR(ENOMEM);
55  avpriv_set_pts_info(video_st, 64, 1, 1000000);
56 
58  if (!audio_st)
59  return AVERROR(ENOMEM);
63  audio_st->codecpar->sample_rate = 8000;
66  avpriv_set_pts_info(audio_st, 64, 1, 1000000);
67 
68  mxg->soi_ptr = mxg->buffer_ptr = mxg->buffer = 0;
69  mxg->buffer_size = 0;
70  mxg->dts = AV_NOPTS_VALUE;
71  mxg->cache_size = 0;
72 
73  return 0;
74 }
75 
76 static uint8_t* mxg_find_startmarker(uint8_t *p, uint8_t *end)
77 {
78  for (; p < end - 3; p += 4) {
79  uint32_t x = AV_RN32(p);
80 
81  if (x & (~(x+0x01010101)) & 0x80808080) {
82  if (p[0] == 0xff) {
83  return p;
84  } else if (p[1] == 0xff) {
85  return p+1;
86  } else if (p[2] == 0xff) {
87  return p+2;
88  } else if (p[3] == 0xff) {
89  return p+3;
90  }
91  }
92  }
93 
94  for (; p < end; ++p) {
95  if (*p == 0xff) return p;
96  }
97 
98  return end;
99 }
100 
101 static int mxg_update_cache(AVFormatContext *s, unsigned int cache_size)
102 {
103  MXGContext *mxg = s->priv_data;
104  unsigned int current_pos = mxg->buffer_ptr - mxg->buffer;
105  unsigned int soi_pos;
106  uint8_t *buffer;
107  int ret;
108 
109  /* reallocate internal buffer */
110  if (current_pos > current_pos + cache_size)
111  return AVERROR(ENOMEM);
112  soi_pos = mxg->soi_ptr - mxg->buffer;
114  current_pos + cache_size +
116  if (!buffer)
117  return AVERROR(ENOMEM);
118  mxg->buffer = buffer;
119  mxg->buffer_ptr = mxg->buffer + current_pos;
120  if (mxg->soi_ptr) mxg->soi_ptr = mxg->buffer + soi_pos;
121 
122  /* get data */
123  ret = avio_read(s->pb, mxg->buffer_ptr + mxg->cache_size,
124  cache_size - mxg->cache_size);
125  if (ret < 0)
126  return ret;
127 
128  mxg->cache_size += ret;
129 
130  return ret;
131 }
132 
134 {
135  int ret;
136  unsigned int size;
137  uint8_t *startmarker_ptr, *end, *search_end, marker;
138  MXGContext *mxg = s->priv_data;
139 
140  while (!avio_feof(s->pb) && !s->pb->error){
141  if (mxg->cache_size <= OVERREAD_SIZE) {
142  /* update internal buffer */
144  if (ret < 0)
145  return ret;
146  }
147  end = mxg->buffer_ptr + mxg->cache_size;
148 
149  /* find start marker - 0xff */
150  if (mxg->cache_size > OVERREAD_SIZE) {
151  search_end = end - OVERREAD_SIZE;
152  startmarker_ptr = mxg_find_startmarker(mxg->buffer_ptr, search_end);
153  } else {
154  search_end = end;
155  startmarker_ptr = mxg_find_startmarker(mxg->buffer_ptr, search_end);
156  if (startmarker_ptr >= search_end - 1 ||
157  *(startmarker_ptr + 1) != EOI) break;
158  }
159 
160  if (startmarker_ptr != search_end) { /* start marker found */
161  marker = *(startmarker_ptr + 1);
162  mxg->buffer_ptr = startmarker_ptr + 2;
163  mxg->cache_size = end - mxg->buffer_ptr;
164 
165  if (marker == SOI) {
166  mxg->soi_ptr = startmarker_ptr;
167  } else if (marker == EOI) {
168  if (!mxg->soi_ptr) {
169  av_log(s, AV_LOG_WARNING, "Found EOI before SOI, skipping\n");
170  continue;
171  }
172 
173  size = mxg->buffer_ptr - mxg->soi_ptr;
175  if (ret < 0)
176  return ret;
177  memcpy(pkt->data, mxg->soi_ptr, size);
178 
179  pkt->pts = pkt->dts = mxg->dts;
180  pkt->stream_index = 0;
181 
182  if (mxg->soi_ptr - mxg->buffer > mxg->cache_size) {
183  if (mxg->cache_size > 0) {
184  memmove(mxg->buffer, mxg->buffer_ptr, mxg->cache_size);
185  }
186 
187  mxg->buffer_ptr = mxg->buffer;
188  }
189  mxg->soi_ptr = 0;
190 
191  return pkt->size;
192  } else if ( (SOF0 <= marker && marker <= SOF15) ||
193  (SOS <= marker && marker <= COM) ) {
194  /* all other markers that start marker segment also contain
195  length value (see specification for JPEG Annex B.1) */
196  size = AV_RB16(mxg->buffer_ptr);
197  if (size < 2)
198  return AVERROR(EINVAL);
199 
200  if (mxg->cache_size < size) {
202  if (ret < 0)
203  return ret;
204  startmarker_ptr = mxg->buffer_ptr - 2;
205  mxg->cache_size = 0;
206  } else {
207  mxg->cache_size -= size;
208  }
209 
210  mxg->buffer_ptr += size;
211 
212  if (marker == APP13 && size >= 16) { /* audio data */
213  ret = av_new_packet(pkt, size - 14);
214  if (ret < 0)
215  return ret;
216  memcpy(pkt->data, startmarker_ptr + 16, size - 14);
217 
218  /* time (GMT) of first sample in usec since 1970, little-endian */
219  pkt->pts = pkt->dts = AV_RL64(startmarker_ptr + 8);
220  pkt->stream_index = 1;
221 
222  if (startmarker_ptr - mxg->buffer > mxg->cache_size) {
223  if (mxg->cache_size > 0) {
224  memcpy(mxg->buffer, mxg->buffer_ptr, mxg->cache_size);
225  }
226  mxg->buffer_ptr = mxg->buffer;
227  }
228 
229  return pkt->size;
230  } else if (marker == COM && size >= 18 &&
231  !strncmp(startmarker_ptr + 4, "MXF", 3)) {
232  /* time (GMT) of video frame in usec since 1970, little-endian */
233  mxg->dts = AV_RL64(startmarker_ptr + 12);
234  }
235  }
236  } else {
237  /* start marker not found */
238  mxg->buffer_ptr = search_end;
239  mxg->cache_size = OVERREAD_SIZE;
240  }
241  }
242 
243  return AVERROR_EOF;
244 }
245 
246 static int mxg_close(struct AVFormatContext *s)
247 {
248  MXGContext *mxg = s->priv_data;
249  av_freep(&mxg->buffer);
250  return 0;
251 }
252 
254  .p.name = "mxg",
255  .p.long_name = NULL_IF_CONFIG_SMALL("MxPEG clip"),
256  .p.extensions = "mxg",
257  .priv_data_size = sizeof(MXGContext),
261 };
ff_mxg_demuxer
const FFInputFormat ff_mxg_demuxer
Definition: mxg.c:253
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
mjpeg.h
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
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
MXGContext::buffer_size
unsigned int buffer_size
Definition: mxg.c:39
SOS
@ SOS
Definition: mjpeg.h:72
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AV_RL64
uint64_t_TMPL AV_RL64
Definition: bytestream.h:91
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
SOF0
@ SOF0
Definition: mjpeg.h:39
int64_t
long long int64_t
Definition: coverity.c:34
AVPacket::data
uint8_t * data
Definition: packet.h:539
mxg_read_header
static int mxg_read_header(AVFormatContext *s)
Definition: mxg.c:44
AV_CODEC_ID_MXPEG
@ AV_CODEC_ID_MXPEG
Definition: codec_id.h:198
MXGContext::buffer
uint8_t * buffer
Definition: mxg.c:36
mxg_update_cache
static int mxg_update_cache(AVFormatContext *s, unsigned int cache_size)
Definition: mxg.c:101
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:862
mxg_find_startmarker
static uint8_t * mxg_find_startmarker(uint8_t *p, uint8_t *end)
Definition: mxg.c:76
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:143
APP13
@ APP13
Definition: mjpeg.h:92
pkt
AVPacket * pkt
Definition: movenc.c:60
COM
@ COM
Definition: mjpeg.h:111
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:42
av_fast_realloc
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition: mem.c:497
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: packet.c:98
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
MXGContext::cache_size
unsigned int cache_size
Definition: mxg.c:41
MXGContext::buffer_ptr
uint8_t * buffer_ptr
Definition: mxg.c:37
OVERREAD_SIZE
#define OVERREAD_SIZE
Definition: mxg.c:33
MXGContext::dts
int64_t dts
Definition: mxg.c:40
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
AV_CODEC_ID_PCM_ALAW
@ AV_CODEC_ID_PCM_ALAW
Definition: codec_id.h:340
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:771
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:540
NULL
#define NULL
Definition: coverity.c:32
AV_RN32
#define AV_RN32(p)
Definition: intreadwrite.h:360
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
audio_st
AVStream * audio_st
Definition: movenc.c:61
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
AVPacket::size
int size
Definition: packet.h:540
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:94
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:311
MXGContext::soi_ptr
uint8_t * soi_ptr
Definition: mxg.c:38
size
int size
Definition: twinvq_data.h:10344
avio.h
video_st
AVStream * video_st
Definition: movenc.c:61
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
SOF15
@ SOF15
Definition: mjpeg.h:54
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:41
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:538
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:532
EOI
@ EOI
Definition: mjpeg.h:71
internal.h
AVCodecParameters::block_align
int block_align
Audio only.
Definition: codec_par.h:191
mxg_read_packet
static int mxg_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mxg.c:133
demux.h
MXGContext
Definition: mxg.c:35
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:748
mxg_close
static int mxg_close(struct AVFormatContext *s)
Definition: mxg.c:246
avformat.h
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
channel_layout.h
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:612
AVPacket::stream_index
int stream_index
Definition: packet.h:541
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:110
mem.h
SOI
@ SOI
Definition: mjpeg.h:70
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:386
DEFAULT_PACKET_SIZE
#define DEFAULT_PACKET_SIZE
Definition: mxg.c:32
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:516
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
FFInputFormat
Definition: demux.h:37
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:98
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:346