Patch by Anatoly Nenashev, anatoly d nenashev a ovsoft d ru
Originally committed as revision 25831 to svn://svn.ffmpeg.org/ffmpeg/trunk
| ... | ... |
@@ -121,6 +121,7 @@ library: |
| 121 | 121 |
@item MAXIS XA @tab @tab X |
| 122 | 122 |
@tab Used in Sim City 3000; file extension .xa. |
| 123 | 123 |
@item MD Studio @tab @tab X |
| 124 |
+@item Mobotix .mxg @tab @tab X |
|
| 124 | 125 |
@item Monkey's Audio @tab @tab X |
| 125 | 126 |
@item Motion Pixels MVI @tab @tab X |
| 126 | 127 |
@item MOV/QuickTime/MP4 @tab X @tab X |
| ... | ... |
@@ -147,6 +147,7 @@ OBJS-$(CONFIG_MTV_DEMUXER) += mtv.o |
| 147 | 147 |
OBJS-$(CONFIG_MVI_DEMUXER) += mvi.o |
| 148 | 148 |
OBJS-$(CONFIG_MXF_DEMUXER) += mxfdec.o mxf.o |
| 149 | 149 |
OBJS-$(CONFIG_MXF_MUXER) += mxfenc.o mxf.o audiointerleave.o |
| 150 |
+OBJS-$(CONFIG_MXG_DEMUXER) += mxg.o |
|
| 150 | 151 |
OBJS-$(CONFIG_NC_DEMUXER) += ncdec.o |
| 151 | 152 |
OBJS-$(CONFIG_NSV_DEMUXER) += nsvdec.o |
| 152 | 153 |
OBJS-$(CONFIG_NULL_MUXER) += nullenc.o |
| ... | ... |
@@ -142,6 +142,7 @@ void av_register_all(void) |
| 142 | 142 |
REGISTER_DEMUXER (MVI, mvi); |
| 143 | 143 |
REGISTER_MUXDEMUX (MXF, mxf); |
| 144 | 144 |
REGISTER_MUXER (MXF_D10, mxf_d10); |
| 145 |
+ REGISTER_DEMUXER (MXG, mxg); |
|
| 145 | 146 |
REGISTER_DEMUXER (NC, nc); |
| 146 | 147 |
REGISTER_DEMUXER (NSV, nsv); |
| 147 | 148 |
REGISTER_MUXER (NULL, null); |
| ... | ... |
@@ -22,8 +22,8 @@ |
| 22 | 22 |
#define AVFORMAT_AVFORMAT_H |
| 23 | 23 |
|
| 24 | 24 |
#define LIBAVFORMAT_VERSION_MAJOR 52 |
| 25 |
-#define LIBAVFORMAT_VERSION_MINOR 86 |
|
| 26 |
-#define LIBAVFORMAT_VERSION_MICRO 1 |
|
| 25 |
+#define LIBAVFORMAT_VERSION_MINOR 87 |
|
| 26 |
+#define LIBAVFORMAT_VERSION_MICRO 0 |
|
| 27 | 27 |
|
| 28 | 28 |
#define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ |
| 29 | 29 |
LIBAVFORMAT_VERSION_MINOR, \ |
| 30 | 30 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,251 @@ |
| 0 |
+/* |
|
| 1 |
+ * MxPEG clip file demuxer |
|
| 2 |
+ * Copyright (c) 2010 Anatoly Nenashev |
|
| 3 |
+ * |
|
| 4 |
+ * This file is part of FFmpeg. |
|
| 5 |
+ * |
|
| 6 |
+ * FFmpeg is free software; you can redistribute it and/or |
|
| 7 |
+ * modify it under the terms of the GNU Lesser General Public |
|
| 8 |
+ * License as published by the Free Software Foundation; either |
|
| 9 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
| 10 |
+ * |
|
| 11 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
| 12 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 13 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
| 14 |
+ * Lesser General Public License for more details. |
|
| 15 |
+ * |
|
| 16 |
+ * You should have received a copy of the GNU Lesser General Public |
|
| 17 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
| 18 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
| 19 |
+ */ |
|
| 20 |
+ |
|
| 21 |
+#include "libavutil/intreadwrite.h" |
|
| 22 |
+#include "libavcodec/mjpeg.h" |
|
| 23 |
+#include "avformat.h" |
|
| 24 |
+#include "avio.h" |
|
| 25 |
+ |
|
| 26 |
+#define VIDEO_STREAM_INDEX 0 |
|
| 27 |
+#define AUDIO_STREAM_INDEX 1 |
|
| 28 |
+#define DEFAULT_PACKET_SIZE 1024 |
|
| 29 |
+#define OVERREAD_SIZE 3 |
|
| 30 |
+ |
|
| 31 |
+typedef struct MXGContext {
|
|
| 32 |
+ uint8_t *buffer; |
|
| 33 |
+ uint8_t *buffer_ptr; |
|
| 34 |
+ uint8_t *soi_ptr; |
|
| 35 |
+ unsigned int buffer_size; |
|
| 36 |
+ int64_t dts; |
|
| 37 |
+ unsigned int cache_size; |
|
| 38 |
+} MXGContext; |
|
| 39 |
+ |
|
| 40 |
+static int mxg_read_header(AVFormatContext *s, AVFormatParameters *ap) |
|
| 41 |
+{
|
|
| 42 |
+ AVStream *video_st, *audio_st; |
|
| 43 |
+ MXGContext *mxg = s->priv_data; |
|
| 44 |
+ |
|
| 45 |
+ /* video parameters will be extracted from the compressed bitstream */ |
|
| 46 |
+ video_st = av_new_stream(s, VIDEO_STREAM_INDEX); |
|
| 47 |
+ if (!video_st) |
|
| 48 |
+ return AVERROR(ENOMEM); |
|
| 49 |
+ video_st->codec->codec_type = AVMEDIA_TYPE_VIDEO; |
|
| 50 |
+ video_st->codec->codec_id = CODEC_ID_MXPEG; |
|
| 51 |
+ av_set_pts_info(video_st, 64, 1, 1000000); |
|
| 52 |
+ |
|
| 53 |
+ audio_st = av_new_stream(s, AUDIO_STREAM_INDEX); |
|
| 54 |
+ if (!audio_st) |
|
| 55 |
+ return AVERROR(ENOMEM); |
|
| 56 |
+ audio_st->codec->codec_type = AVMEDIA_TYPE_AUDIO; |
|
| 57 |
+ audio_st->codec->codec_id = CODEC_ID_PCM_ALAW; |
|
| 58 |
+ audio_st->codec->channels = 1; |
|
| 59 |
+ audio_st->codec->sample_rate = 8000; |
|
| 60 |
+ audio_st->codec->bits_per_coded_sample = 8; |
|
| 61 |
+ audio_st->codec->block_align = 1; |
|
| 62 |
+ av_set_pts_info(audio_st, 64, 1, 1000000); |
|
| 63 |
+ |
|
| 64 |
+ mxg->soi_ptr = mxg->buffer_ptr = mxg->buffer = 0; |
|
| 65 |
+ mxg->buffer_size = 0; |
|
| 66 |
+ mxg->dts = AV_NOPTS_VALUE; |
|
| 67 |
+ mxg->cache_size = 0; |
|
| 68 |
+ |
|
| 69 |
+ return 0; |
|
| 70 |
+} |
|
| 71 |
+ |
|
| 72 |
+static uint8_t* mxg_find_startmarker(uint8_t *p, uint8_t *end) |
|
| 73 |
+{
|
|
| 74 |
+ for (; p < end - 3; p += 4) {
|
|
| 75 |
+ uint32_t x = *(uint32_t*)p; |
|
| 76 |
+ |
|
| 77 |
+ if (x & (~(x+0x01010101)) & 0x80808080) {
|
|
| 78 |
+ if (p[0] == 0xff) {
|
|
| 79 |
+ return p; |
|
| 80 |
+ } else if (p[1] == 0xff) {
|
|
| 81 |
+ return p+1; |
|
| 82 |
+ } else if (p[2] == 0xff) {
|
|
| 83 |
+ return p+2; |
|
| 84 |
+ } else if (p[3] == 0xff) {
|
|
| 85 |
+ return p+3; |
|
| 86 |
+ } |
|
| 87 |
+ } |
|
| 88 |
+ } |
|
| 89 |
+ |
|
| 90 |
+ for (; p < end; ++p) {
|
|
| 91 |
+ if (*p == 0xff) return p; |
|
| 92 |
+ } |
|
| 93 |
+ |
|
| 94 |
+ return end; |
|
| 95 |
+} |
|
| 96 |
+ |
|
| 97 |
+static int mxg_update_cache(AVFormatContext *s, unsigned int cache_size) |
|
| 98 |
+{
|
|
| 99 |
+ MXGContext *mxg = s->priv_data; |
|
| 100 |
+ unsigned int current_pos = mxg->buffer_ptr - mxg->buffer; |
|
| 101 |
+ unsigned int soi_pos; |
|
| 102 |
+ int ret; |
|
| 103 |
+ |
|
| 104 |
+ /* reallocate internal buffer */ |
|
| 105 |
+ if (current_pos > current_pos + cache_size) |
|
| 106 |
+ return AVERROR(ENOMEM); |
|
| 107 |
+ if (mxg->soi_ptr) soi_pos = mxg->soi_ptr - mxg->buffer; |
|
| 108 |
+ mxg->buffer = av_fast_realloc(mxg->buffer, &mxg->buffer_size, |
|
| 109 |
+ current_pos + cache_size + |
|
| 110 |
+ FF_INPUT_BUFFER_PADDING_SIZE); |
|
| 111 |
+ if (!mxg->buffer) |
|
| 112 |
+ return AVERROR(ENOMEM); |
|
| 113 |
+ mxg->buffer_ptr = mxg->buffer + current_pos; |
|
| 114 |
+ if (mxg->soi_ptr) mxg->soi_ptr = mxg->buffer + soi_pos; |
|
| 115 |
+ |
|
| 116 |
+ /* get data */ |
|
| 117 |
+ ret = get_buffer(s->pb, mxg->buffer_ptr + mxg->cache_size, |
|
| 118 |
+ cache_size - mxg->cache_size); |
|
| 119 |
+ if (ret < 0) |
|
| 120 |
+ return ret; |
|
| 121 |
+ |
|
| 122 |
+ mxg->cache_size += ret; |
|
| 123 |
+ |
|
| 124 |
+ return ret; |
|
| 125 |
+} |
|
| 126 |
+ |
|
| 127 |
+static int mxg_read_packet(AVFormatContext *s, AVPacket *pkt) |
|
| 128 |
+{
|
|
| 129 |
+ int ret; |
|
| 130 |
+ unsigned int size; |
|
| 131 |
+ uint8_t *startmarker_ptr, *end, *search_end, marker; |
|
| 132 |
+ MXGContext *mxg = s->priv_data; |
|
| 133 |
+ |
|
| 134 |
+ while (!url_feof(s->pb) && !url_ferror(s->pb)){
|
|
| 135 |
+ if (mxg->cache_size <= OVERREAD_SIZE) {
|
|
| 136 |
+ /* update internal buffer */ |
|
| 137 |
+ ret = mxg_update_cache(s, DEFAULT_PACKET_SIZE + OVERREAD_SIZE); |
|
| 138 |
+ if (ret < 0) |
|
| 139 |
+ return ret; |
|
| 140 |
+ } |
|
| 141 |
+ end = mxg->buffer_ptr + mxg->cache_size; |
|
| 142 |
+ |
|
| 143 |
+ /* find start marker - 0xff */ |
|
| 144 |
+ if (mxg->cache_size > OVERREAD_SIZE) {
|
|
| 145 |
+ search_end = end - OVERREAD_SIZE; |
|
| 146 |
+ startmarker_ptr = mxg_find_startmarker(mxg->buffer_ptr, search_end); |
|
| 147 |
+ } else {
|
|
| 148 |
+ search_end = end; |
|
| 149 |
+ startmarker_ptr = mxg_find_startmarker(mxg->buffer_ptr, search_end); |
|
| 150 |
+ if (startmarker_ptr >= search_end - 1 || |
|
| 151 |
+ *(startmarker_ptr + 1) != EOI) break; |
|
| 152 |
+ } |
|
| 153 |
+ |
|
| 154 |
+ if (startmarker_ptr != search_end) { /* start marker found */
|
|
| 155 |
+ marker = *(startmarker_ptr + 1); |
|
| 156 |
+ mxg->buffer_ptr = startmarker_ptr + 2; |
|
| 157 |
+ mxg->cache_size = end - mxg->buffer_ptr; |
|
| 158 |
+ |
|
| 159 |
+ if (marker == SOI) {
|
|
| 160 |
+ mxg->soi_ptr = startmarker_ptr; |
|
| 161 |
+ } else if (marker == EOI) {
|
|
| 162 |
+ if (!mxg->soi_ptr) {
|
|
| 163 |
+ av_log(s, AV_LOG_WARNING, "Found EOI before SOI, skipping\n"); |
|
| 164 |
+ continue; |
|
| 165 |
+ } |
|
| 166 |
+ |
|
| 167 |
+ pkt->pts = pkt->dts = mxg->dts; |
|
| 168 |
+ pkt->stream_index = VIDEO_STREAM_INDEX; |
|
| 169 |
+ pkt->destruct = NULL; |
|
| 170 |
+ pkt->size = mxg->buffer_ptr - mxg->soi_ptr; |
|
| 171 |
+ pkt->data = mxg->soi_ptr; |
|
| 172 |
+ |
|
| 173 |
+ if (mxg->soi_ptr - mxg->buffer > mxg->cache_size) {
|
|
| 174 |
+ if (mxg->cache_size > 0) {
|
|
| 175 |
+ memcpy(mxg->buffer, mxg->buffer_ptr, mxg->cache_size); |
|
| 176 |
+ } |
|
| 177 |
+ |
|
| 178 |
+ mxg->buffer_ptr = mxg->buffer; |
|
| 179 |
+ } |
|
| 180 |
+ mxg->soi_ptr = 0; |
|
| 181 |
+ |
|
| 182 |
+ return pkt->size; |
|
| 183 |
+ } else if ( (SOF0 <= marker && marker <= SOF15) || |
|
| 184 |
+ (SOS <= marker && marker <= COM) ) {
|
|
| 185 |
+ /* all other markers that start marker segment also contain |
|
| 186 |
+ length value (see specification for JPEG Annex B.1) */ |
|
| 187 |
+ size = AV_RB16(mxg->buffer_ptr); |
|
| 188 |
+ if (size < 2) |
|
| 189 |
+ return AVERROR(EINVAL); |
|
| 190 |
+ |
|
| 191 |
+ if (mxg->cache_size < size) {
|
|
| 192 |
+ ret = mxg_update_cache(s, size); |
|
| 193 |
+ if (ret < 0) |
|
| 194 |
+ return ret; |
|
| 195 |
+ startmarker_ptr = mxg->buffer_ptr - 2; |
|
| 196 |
+ mxg->cache_size = 0; |
|
| 197 |
+ } else {
|
|
| 198 |
+ mxg->cache_size -= size; |
|
| 199 |
+ } |
|
| 200 |
+ |
|
| 201 |
+ mxg->buffer_ptr += size; |
|
| 202 |
+ |
|
| 203 |
+ if (marker == APP13 && size >= 16) { /* audio data */
|
|
| 204 |
+ /* time (GMT) of first sample in usec since 1970, little-endian */ |
|
| 205 |
+ pkt->pts = pkt->dts = AV_RL64(startmarker_ptr + 8); |
|
| 206 |
+ pkt->stream_index = AUDIO_STREAM_INDEX; |
|
| 207 |
+ pkt->destruct = NULL; |
|
| 208 |
+ pkt->size = size - 14; |
|
| 209 |
+ pkt->data = startmarker_ptr + 16; |
|
| 210 |
+ |
|
| 211 |
+ if (startmarker_ptr - mxg->buffer > mxg->cache_size) {
|
|
| 212 |
+ if (mxg->cache_size > 0) {
|
|
| 213 |
+ memcpy(mxg->buffer, mxg->buffer_ptr, mxg->cache_size); |
|
| 214 |
+ } |
|
| 215 |
+ mxg->buffer_ptr = mxg->buffer; |
|
| 216 |
+ } |
|
| 217 |
+ |
|
| 218 |
+ return pkt->size; |
|
| 219 |
+ } else if (marker == COM && size >= 18 && |
|
| 220 |
+ !strncmp(startmarker_ptr + 4, "MXF", 3)) {
|
|
| 221 |
+ /* time (GMT) of video frame in usec since 1970, little-endian */ |
|
| 222 |
+ mxg->dts = AV_RL64(startmarker_ptr + 12); |
|
| 223 |
+ } |
|
| 224 |
+ } |
|
| 225 |
+ } else {
|
|
| 226 |
+ /* start marker not found */ |
|
| 227 |
+ mxg->buffer_ptr = search_end; |
|
| 228 |
+ mxg->cache_size = OVERREAD_SIZE; |
|
| 229 |
+ } |
|
| 230 |
+ } |
|
| 231 |
+ |
|
| 232 |
+ return AVERROR_EOF; |
|
| 233 |
+} |
|
| 234 |
+ |
|
| 235 |
+static int mxg_close(struct AVFormatContext *s) |
|
| 236 |
+{
|
|
| 237 |
+ MXGContext *mxg = s->priv_data; |
|
| 238 |
+ av_freep(&mxg->buffer); |
|
| 239 |
+ return 0; |
|
| 240 |
+} |
|
| 241 |
+ |
|
| 242 |
+AVInputFormat mxg_demuxer = {
|
|
| 243 |
+ .name = "mxg", |
|
| 244 |
+ .long_name = NULL_IF_CONFIG_SMALL("MxPEG clip file format"),
|
|
| 245 |
+ .priv_data_size = sizeof(MXGContext), |
|
| 246 |
+ .read_header = mxg_read_header, |
|
| 247 |
+ .read_packet = mxg_read_packet, |
|
| 248 |
+ .read_close = mxg_close, |
|
| 249 |
+ .extensions = "mxg" |
|
| 250 |
+}; |