Browse code

Merge commit 'e7a44f87d07655ec0cd31c315936931674434340'

* commit 'e7a44f87d07655ec0cd31c315936931674434340':
4xm: refactor fourxm_read_header

Conflicts:
libavformat/4xm.c

Merged-by: Michael Niedermayer <michaelni@gmx.at>

Michael Niedermayer authored on 2013/06/13 17:08:50
Showing 2 changed files
... ...
@@ -411,12 +411,14 @@ static int decode_p_frame(FourXContext *f, AVFrame *frame,
411 411
     int x, y;
412 412
     const int width  = f->avctx->width;
413 413
     const int height = f->avctx->height;
414
-    uint16_t *src    = (uint16_t *)f->last_picture->data[0];
415 414
     uint16_t *dst    = (uint16_t *)frame->data[0];
416 415
     const int stride =             frame->linesize[0] >> 1;
416
+    uint16_t *src;
417 417
     unsigned int bitstream_size, bytestream_size, wordstream_size, extra,
418 418
                  bytestream_offset, wordstream_offset;
419 419
 
420
+    src = (uint16_t *)f->last_picture->data[0];
421
+
420 422
     if (f->version > 1) {
421 423
         extra           = 20;
422 424
         if (length < extra)
... ...
@@ -72,8 +72,6 @@ typedef struct AudioTrack {
72 72
 } AudioTrack;
73 73
 
74 74
 typedef struct FourxmDemuxContext {
75
-    int width;
76
-    int height;
77 75
     int video_stream_index;
78 76
     int track_count;
79 77
     AudioTrack *tracks;
... ...
@@ -91,6 +89,105 @@ static int fourxm_probe(AVProbeData *p)
91 91
     return AVPROBE_SCORE_MAX;
92 92
 }
93 93
 
94
+static int parse_vtrk(AVFormatContext *s,
95
+                      FourxmDemuxContext *fourxm, uint8_t *buf, int size)
96
+{
97
+    AVStream *st;
98
+    /* check that there is enough data */
99
+    if (size != vtrk_SIZE) {
100
+        return AVERROR_INVALIDDATA;
101
+    }
102
+
103
+    /* allocate a new AVStream */
104
+    st = avformat_new_stream(s, NULL);
105
+    if (!st)
106
+        return AVERROR(ENOMEM);
107
+
108
+    avpriv_set_pts_info(st, 60, 1, fourxm->fps);
109
+
110
+    fourxm->video_stream_index = st->index;
111
+
112
+    st->codec->codec_type     = AVMEDIA_TYPE_VIDEO;
113
+    st->codec->codec_id       = AV_CODEC_ID_4XM;
114
+    st->codec->extradata_size = 4;
115
+    st->codec->extradata      = av_malloc(4);
116
+    AV_WL32(st->codec->extradata, AV_RL32(buf + 16));
117
+    st->codec->width  = AV_RL32(buf + 36);
118
+    st->codec->height = AV_RL32(buf + 40);
119
+
120
+    return 0;
121
+}
122
+
123
+
124
+static int parse_strk(AVFormatContext *s,
125
+                      FourxmDemuxContext *fourxm, uint8_t *buf, int size)
126
+{
127
+    AVStream *st;
128
+    int track;
129
+    /* check that there is enough data */
130
+    if (size != strk_SIZE)
131
+        return AVERROR_INVALIDDATA;
132
+
133
+    track = AV_RL32(buf + 8);
134
+    if ((unsigned)track >= UINT_MAX / sizeof(AudioTrack) - 1) {
135
+        av_log(s, AV_LOG_ERROR, "current_track too large\n");
136
+        return AVERROR_INVALIDDATA;
137
+    }
138
+    if (track + 1 > fourxm->track_count) {
139
+        if (av_reallocp_array(&fourxm->tracks, track + 1, sizeof(AudioTrack)))
140
+            return AVERROR(ENOMEM);
141
+        memset(&fourxm->tracks[fourxm->track_count], 0,
142
+               sizeof(AudioTrack) * (track + 1 - fourxm->track_count));
143
+        fourxm->track_count = track + 1;
144
+    }
145
+    fourxm->tracks[track].adpcm       = AV_RL32(buf + 12);
146
+    fourxm->tracks[track].channels    = AV_RL32(buf + 36);
147
+    fourxm->tracks[track].sample_rate = AV_RL32(buf + 40);
148
+    fourxm->tracks[track].bits        = AV_RL32(buf + 44);
149
+    fourxm->tracks[track].audio_pts   = 0;
150
+
151
+    if (fourxm->tracks[track].channels    <= 0 ||
152
+        fourxm->tracks[track].sample_rate <= 0 ||
153
+        fourxm->tracks[track].bits        < 0) {
154
+        av_log(s, AV_LOG_ERROR, "audio header invalid\n");
155
+        return AVERROR_INVALIDDATA;
156
+    }
157
+    if (!fourxm->tracks[track].adpcm && fourxm->tracks[track].bits<8) {
158
+        av_log(s, AV_LOG_ERROR, "bits unspecified for non ADPCM\n");
159
+        return AVERROR_INVALIDDATA;
160
+    }
161
+
162
+    /* allocate a new AVStream */
163
+    st = avformat_new_stream(s, NULL);
164
+    if (!st)
165
+        return AVERROR(ENOMEM);
166
+
167
+    st->id = track;
168
+    avpriv_set_pts_info(st, 60, 1, fourxm->tracks[track].sample_rate);
169
+
170
+    fourxm->tracks[track].stream_index = st->index;
171
+
172
+    st->codec->codec_type            = AVMEDIA_TYPE_AUDIO;
173
+    st->codec->codec_tag             = 0;
174
+    st->codec->channels              = fourxm->tracks[track].channels;
175
+    st->codec->sample_rate           = fourxm->tracks[track].sample_rate;
176
+    st->codec->bits_per_coded_sample = fourxm->tracks[track].bits;
177
+    st->codec->bit_rate              = st->codec->channels *
178
+                                       st->codec->sample_rate *
179
+                                       st->codec->bits_per_coded_sample;
180
+    st->codec->block_align           = st->codec->channels *
181
+                                       st->codec->bits_per_coded_sample;
182
+
183
+    if (fourxm->tracks[track].adpcm){
184
+        st->codec->codec_id = AV_CODEC_ID_ADPCM_4XM;
185
+    } else if (st->codec->bits_per_coded_sample == 8) {
186
+        st->codec->codec_id = AV_CODEC_ID_PCM_U8;
187
+    } else
188
+        st->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
189
+
190
+    return 0;
191
+}
192
+
94 193
 static int fourxm_read_header(AVFormatContext *s)
95 194
 {
96 195
     AVIOContext *pb = s->pb;
... ...
@@ -100,7 +197,6 @@ static int fourxm_read_header(AVFormatContext *s)
100 100
     FourxmDemuxContext *fourxm = s->priv_data;
101 101
     unsigned char *header;
102 102
     int i, ret;
103
-    AVStream *st;
104 103
 
105 104
     fourxm->track_count = 0;
106 105
     fourxm->tracks      = NULL;
... ...
@@ -140,104 +236,15 @@ static int fourxm_read_header(AVFormatContext *s)
140 140
             }
141 141
             fourxm->fps = av_int2float(AV_RL32(&header[i + 12]));
142 142
         } else if (fourcc_tag == vtrk_TAG) {
143
-            /* check that there is enough data */
144
-            if (size != vtrk_SIZE) {
145
-                ret = AVERROR_INVALIDDATA;
143
+            if ((ret = parse_vtrk(s, fourxm, header + i, size)) < 0)
146 144
                 goto fail;
147
-            }
148
-            fourxm->width  = AV_RL32(&header[i + 36]);
149
-            fourxm->height = AV_RL32(&header[i + 40]);
150
-
151
-            /* allocate a new AVStream */
152
-            st = avformat_new_stream(s, NULL);
153
-            if (!st) {
154
-                ret = AVERROR(ENOMEM);
155
-                goto fail;
156
-            }
157
-            avpriv_set_pts_info(st, 60, 1, fourxm->fps);
158
-
159
-            fourxm->video_stream_index = st->index;
160
-
161
-            st->codec->codec_type     = AVMEDIA_TYPE_VIDEO;
162
-            st->codec->codec_id       = AV_CODEC_ID_4XM;
163
-            st->codec->extradata_size = 4;
164
-            st->codec->extradata      = av_malloc(4);
165
-            AV_WL32(st->codec->extradata, AV_RL32(&header[i + 16]));
166
-            st->codec->width  = fourxm->width;
167
-            st->codec->height = fourxm->height;
168 145
 
169 146
             i += 8 + size;
170 147
         } else if (fourcc_tag == strk_TAG) {
171
-            int current_track;
172
-            /* check that there is enough data */
173
-            if (size != strk_SIZE) {
174
-                ret= AVERROR_INVALIDDATA;
175
-                goto fail;
176
-            }
177
-            current_track = AV_RL32(&header[i + 8]);
178
-            if((unsigned)current_track >= UINT_MAX / sizeof(AudioTrack) - 1){
179
-                av_log(s, AV_LOG_ERROR, "current_track too large\n");
180
-                ret = AVERROR_INVALIDDATA;
181
-                goto fail;
182
-            }
183
-            if (current_track + 1 > fourxm->track_count) {
184
-                fourxm->tracks = av_realloc_f(fourxm->tracks,
185
-                                              sizeof(AudioTrack),
186
-                                              current_track + 1);
187
-                if (!fourxm->tracks) {
188
-                    ret = AVERROR(ENOMEM);
189
-                    goto fail;
190
-                }
191
-                memset(&fourxm->tracks[fourxm->track_count], 0,
192
-                       sizeof(AudioTrack) * (current_track + 1 - fourxm->track_count));
193
-                fourxm->track_count = current_track + 1;
194
-            }
195
-            fourxm->tracks[current_track].adpcm       = AV_RL32(&header[i + 12]);
196
-            fourxm->tracks[current_track].channels    = AV_RL32(&header[i + 36]);
197
-            fourxm->tracks[current_track].sample_rate = AV_RL32(&header[i + 40]);
198
-            fourxm->tracks[current_track].bits        = AV_RL32(&header[i + 44]);
199
-            fourxm->tracks[current_track].audio_pts   = 0;
200
-            if (fourxm->tracks[current_track].channels    <= 0 ||
201
-                fourxm->tracks[current_track].sample_rate <= 0 ||
202
-                fourxm->tracks[current_track].bits        < 0) {
203
-                av_log(s, AV_LOG_ERROR, "audio header invalid\n");
204
-                ret = AVERROR_INVALIDDATA;
148
+            if ((ret = parse_strk(s, fourxm, header + i, size)) < 0)
205 149
                 goto fail;
206
-            }
207
-            if(!fourxm->tracks[current_track].adpcm && fourxm->tracks[current_track].bits<8){
208
-                av_log(s, AV_LOG_ERROR, "bits unspecified for non ADPCM\n");
209
-                ret = AVERROR_INVALIDDATA;
210
-                goto fail;
211
-            }
212
-            i += 8 + size;
213
-
214
-            /* allocate a new AVStream */
215
-            st = avformat_new_stream(s, NULL);
216
-            if (!st) {
217
-                ret = AVERROR(ENOMEM);
218
-                goto fail;
219
-            }
220 150
 
221
-            st->id = current_track;
222
-            avpriv_set_pts_info(st, 60, 1,
223
-                                fourxm->tracks[current_track].sample_rate);
224
-
225
-            fourxm->tracks[current_track].stream_index = st->index;
226
-
227
-            st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
228
-            st->codec->codec_tag = 0;
229
-            st->codec->channels              = fourxm->tracks[current_track].channels;
230
-            st->codec->sample_rate           = fourxm->tracks[current_track].sample_rate;
231
-            st->codec->bits_per_coded_sample = fourxm->tracks[current_track].bits;
232
-            st->codec->bit_rate              = st->codec->channels * st->codec->sample_rate *
233
-                st->codec->bits_per_coded_sample;
234
-            st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
235
-            if (fourxm->tracks[current_track].adpcm){
236
-                st->codec->codec_id = AV_CODEC_ID_ADPCM_4XM;
237
-            } else if (st->codec->bits_per_coded_sample == 8) {
238
-                st->codec->codec_id = AV_CODEC_ID_PCM_U8;
239
-            } else
240
-                st->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
151
+            i += 8 + size;
241 152
         }
242 153
     }
243 154