annotate ffm.c @ 538:d2cc9c6f5d98 libavformat

Help debugging by keeping symbols ans line numbers patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
author michael
date Fri, 01 Oct 2004 13:32:13 +0000
parents 558a093b04db
children 26a477a5ebda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1 /*
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
2 * FFM (ffserver live feed) encoder and decoder
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
3 * Copyright (c) 2001 Fabrice Bellard.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
4 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
8 * version 2 of the License, or (at your option) any later version.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
9 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
13 * Lesser General Public License for more details.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
14 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
18 */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
19 #include "avformat.h"
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
20 #include <unistd.h>
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
21
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
22 /* The FFM file is made of blocks of fixed size */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
23 #define FFM_HEADER_SIZE 14
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
24 #define PACKET_ID 0x666d
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
25
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
26 /* each packet contains frames (which can span several packets */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
27 #define FRAME_HEADER_SIZE 8
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
28 #define FLAG_KEY_FRAME 0x01
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
29
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
30 typedef struct FFMStream {
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 34
diff changeset
31 int64_t pts;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
32 } FFMStream;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
33
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
34 enum {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
35 READ_HEADER,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
36 READ_DATA,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
37 };
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
38
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
39 typedef struct FFMContext {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
40 /* only reading mode */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
41 offset_t write_index, file_size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
42 int read_state;
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 34
diff changeset
43 uint8_t header[FRAME_HEADER_SIZE];
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
44
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
45 /* read and write */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
46 int first_packet; /* true if first packet, needed to set the discontinuity tag */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
47 int packet_size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
48 int frame_offset;
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 34
diff changeset
49 int64_t pts;
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 34
diff changeset
50 uint8_t *packet_ptr, *packet_end;
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 34
diff changeset
51 uint8_t packet[FFM_PACKET_SIZE];
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
52 } FFMContext;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
53
318
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
54 static int64_t get_pts(AVFormatContext *s, offset_t pos);
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
55
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
56 /* disable pts hack for testing */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
57 int ffm_nopts = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
58
277
a313e1080322 disable encoders where appropriate (patch courtesy of BERO
melanson
parents: 241
diff changeset
59 #ifdef CONFIG_ENCODERS
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
60 static void flush_packet(AVFormatContext *s)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
61 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
62 FFMContext *ffm = s->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
63 int fill_size, h;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
64 ByteIOContext *pb = &s->pb;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
65
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
66 fill_size = ffm->packet_end - ffm->packet_ptr;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
67 memset(ffm->packet_ptr, 0, fill_size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
68
318
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
69 if (url_ftell(pb) % ffm->packet_size)
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
70 av_abort();
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
71
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
72 /* put header */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
73 put_be16(pb, PACKET_ID);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
74 put_be16(pb, fill_size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
75 put_be64(pb, ffm->pts);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
76 h = ffm->frame_offset;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
77 if (ffm->first_packet)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
78 h |= 0x8000;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
79 put_be16(pb, h);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
80 put_buffer(pb, ffm->packet, ffm->packet_end - ffm->packet);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
81
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
82 /* prepare next packet */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
83 ffm->frame_offset = 0; /* no key frame */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
84 ffm->pts = 0; /* no pts */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
85 ffm->packet_ptr = ffm->packet;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
86 ffm->first_packet = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
87 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
88
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
89 /* 'first' is true if first data of a frame */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
90 static void ffm_write_data(AVFormatContext *s,
241
3d92f793fd67 64 bit pts for writing - more const usage
bellard
parents: 187
diff changeset
91 const uint8_t *buf, int size,
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 34
diff changeset
92 int64_t pts, int first)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
93 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
94 FFMContext *ffm = s->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
95 int len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
96
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
97 if (first && ffm->frame_offset == 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
98 ffm->frame_offset = ffm->packet_ptr - ffm->packet + FFM_HEADER_SIZE;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
99 if (first && ffm->pts == 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
100 ffm->pts = pts;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
101
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
102 /* write as many packets as needed */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
103 while (size > 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
104 len = ffm->packet_end - ffm->packet_ptr;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
105 if (len > size)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
106 len = size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
107 memcpy(ffm->packet_ptr, buf, len);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
108
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
109 ffm->packet_ptr += len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
110 buf += len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
111 size -= len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
112 if (ffm->packet_ptr >= ffm->packet_end) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
113 /* special case : no pts in packet : we leave the current one */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
114 if (ffm->pts == 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
115 ffm->pts = pts;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
116
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
117 flush_packet(s);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
118 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
119 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
120 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
121
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
122 static int ffm_write_header(AVFormatContext *s)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
123 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
124 FFMContext *ffm = s->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
125 AVStream *st;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
126 FFMStream *fst;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
127 ByteIOContext *pb = &s->pb;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
128 AVCodecContext *codec;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
129 int bit_rate, i;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
130
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
131 ffm->packet_size = FFM_PACKET_SIZE;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
132
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
133 /* header */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
134 put_tag(pb, "FFM1");
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
135 put_be32(pb, ffm->packet_size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
136 /* XXX: store write position in other file ? */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
137 put_be64(pb, ffm->packet_size); /* current write position */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
138
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
139 put_be32(pb, s->nb_streams);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
140 bit_rate = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
141 for(i=0;i<s->nb_streams;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
142 st = s->streams[i];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
143 bit_rate += st->codec.bit_rate;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
144 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
145 put_be32(pb, bit_rate);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
146
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
147 /* list of streams */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
148 for(i=0;i<s->nb_streams;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
149 st = s->streams[i];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
150 fst = av_mallocz(sizeof(FFMStream));
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
151 if (!fst)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
152 goto fail;
502
813b0119a98e ffserver fixes by (Koos Vriezen <koos.vriezen at xs4all dot nl>)
michael
parents: 468
diff changeset
153 av_set_pts_info(st, 64, 1, 1000000);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
154 st->priv_data = fst;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
155
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
156 codec = &st->codec;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
157 /* generic info */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
158 put_be32(pb, codec->codec_id);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
159 put_byte(pb, codec->codec_type);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
160 put_be32(pb, codec->bit_rate);
5
39c4c4336486 cleanup
michaelni
parents: 0
diff changeset
161 put_be32(pb, st->quality);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
162 put_be32(pb, codec->flags);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
163 /* specific info */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
164 switch(codec->codec_type) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
165 case CODEC_TYPE_VIDEO:
85
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 79
diff changeset
166 put_be32(pb, codec->frame_rate_base);
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 79
diff changeset
167 put_be32(pb, codec->frame_rate);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
168 put_be16(pb, codec->width);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
169 put_be16(pb, codec->height);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
170 put_be16(pb, codec->gop_size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
171 put_byte(pb, codec->qmin);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
172 put_byte(pb, codec->qmax);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
173 put_byte(pb, codec->max_qdiff);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
174 put_be16(pb, (int) (codec->qcompress * 10000.0));
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
175 put_be16(pb, (int) (codec->qblur * 10000.0));
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
176 put_be32(pb, codec->bit_rate_tolerance);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
177 put_strz(pb, codec->rc_eq);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
178 put_be32(pb, codec->rc_max_rate);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
179 put_be32(pb, codec->rc_min_rate);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
180 put_be32(pb, codec->rc_buffer_size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
181 put_be64_double(pb, codec->i_quant_factor);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
182 put_be64_double(pb, codec->b_quant_factor);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
183 put_be64_double(pb, codec->i_quant_offset);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
184 put_be64_double(pb, codec->b_quant_offset);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
185 put_be32(pb, codec->dct_algo);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
186 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
187 case CODEC_TYPE_AUDIO:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
188 put_be32(pb, codec->sample_rate);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
189 put_le16(pb, codec->channels);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
190 put_le16(pb, codec->frame_size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
191 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
192 default:
537
558a093b04db do not call (av_)abort()
michael
parents: 502
diff changeset
193 return -1;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
194 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
195 /* hack to have real time */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
196 if (ffm_nopts)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
197 fst->pts = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
198 else
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
199 fst->pts = av_gettime();
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
200 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
201
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
202 /* flush until end of block reached */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
203 while ((url_ftell(pb) % ffm->packet_size) != 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
204 put_byte(pb, 0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
205
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
206 put_flush_packet(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
207
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
208 /* init packet mux */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
209 ffm->packet_ptr = ffm->packet;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
210 ffm->packet_end = ffm->packet + ffm->packet_size - FFM_HEADER_SIZE;
537
558a093b04db do not call (av_)abort()
michael
parents: 502
diff changeset
211 assert(ffm->packet_end >= ffm->packet);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
212 ffm->frame_offset = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
213 ffm->pts = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
214 ffm->first_packet = 1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
215
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
216 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
217 fail:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
218 for(i=0;i<s->nb_streams;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
219 st = s->streams[i];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
220 av_freep(&st->priv_data);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
221 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
222 return -1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
223 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
224
468
60f897e8dd2d pass AVPacket into av_write_frame()
michael
parents: 390
diff changeset
225 static int ffm_write_packet(AVFormatContext *s, AVPacket *pkt)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
226 {
468
60f897e8dd2d pass AVPacket into av_write_frame()
michael
parents: 390
diff changeset
227 AVStream *st = s->streams[pkt->stream_index];
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
228 FFMStream *fst = st->priv_data;
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 34
diff changeset
229 int64_t pts;
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 34
diff changeset
230 uint8_t header[FRAME_HEADER_SIZE];
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
231 int duration;
468
60f897e8dd2d pass AVPacket into av_write_frame()
michael
parents: 390
diff changeset
232 int size= pkt->size;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
233
468
60f897e8dd2d pass AVPacket into av_write_frame()
michael
parents: 390
diff changeset
234 //XXX/FIXME use duration from pkt
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
235 if (st->codec.codec_type == CODEC_TYPE_AUDIO) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
236 duration = ((float)st->codec.frame_size / st->codec.sample_rate * 1000000.0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
237 } else {
85
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 79
diff changeset
238 duration = (1000000.0 * st->codec.frame_rate_base / (float)st->codec.frame_rate);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
239 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
240
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
241 pts = fst->pts;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
242 /* packet size & key_frame */
468
60f897e8dd2d pass AVPacket into av_write_frame()
michael
parents: 390
diff changeset
243 header[0] = pkt->stream_index;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
244 header[1] = 0;
468
60f897e8dd2d pass AVPacket into av_write_frame()
michael
parents: 390
diff changeset
245 if (pkt->flags & PKT_FLAG_KEY)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
246 header[1] |= FLAG_KEY_FRAME;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
247 header[2] = (size >> 16) & 0xff;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
248 header[3] = (size >> 8) & 0xff;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
249 header[4] = size & 0xff;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
250 header[5] = (duration >> 16) & 0xff;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
251 header[6] = (duration >> 8) & 0xff;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
252 header[7] = duration & 0xff;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
253 ffm_write_data(s, header, FRAME_HEADER_SIZE, pts, 1);
468
60f897e8dd2d pass AVPacket into av_write_frame()
michael
parents: 390
diff changeset
254 ffm_write_data(s, pkt->data, size, pts, 0);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
255
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
256 fst->pts += duration;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
257 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
258 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
259
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
260 static int ffm_write_trailer(AVFormatContext *s)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
261 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
262 ByteIOContext *pb = &s->pb;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
263 FFMContext *ffm = s->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
264 int i;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
265
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
266 /* flush packets */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
267 if (ffm->packet_ptr > ffm->packet)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
268 flush_packet(s);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
269
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
270 put_flush_packet(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
271
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
272 if (!url_is_streamed(pb)) {
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 34
diff changeset
273 int64_t size;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
274 /* update the write offset */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
275 size = url_ftell(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
276 url_fseek(pb, 8, SEEK_SET);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
277 put_be64(pb, size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
278 put_flush_packet(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
279 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
280
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
281 for(i=0;i<s->nb_streams;i++)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
282 av_freep(&s->streams[i]->priv_data);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
283 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
284 }
277
a313e1080322 disable encoders where appropriate (patch courtesy of BERO
melanson
parents: 241
diff changeset
285 #endif //CONFIG_ENCODERS
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
286
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
287 /* ffm demux */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
288
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
289 static int ffm_is_avail_data(AVFormatContext *s, int size)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
290 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
291 FFMContext *ffm = s->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
292 offset_t pos, avail_size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
293 int len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
294
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
295 len = ffm->packet_end - ffm->packet_ptr;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
296 if (!ffm_nopts) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
297 /* XXX: I don't understand this test, so I disabled it for testing */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
298 if (size <= len)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
299 return 1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
300 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
301 pos = url_ftell(&s->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
302 if (pos == ffm->write_index) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
303 /* exactly at the end of stream */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
304 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
305 } else if (pos < ffm->write_index) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
306 avail_size = ffm->write_index - pos;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
307 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
308 avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
309 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
310 avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
311 if (size <= avail_size)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
312 return 1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
313 else
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
314 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
315 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
316
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
317 /* first is true if we read the frame header */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
318 static int ffm_read_data(AVFormatContext *s,
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 34
diff changeset
319 uint8_t *buf, int size, int first)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
320 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
321 FFMContext *ffm = s->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
322 ByteIOContext *pb = &s->pb;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
323 int len, fill_size, size1, frame_offset;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
324
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
325 size1 = size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
326 while (size > 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
327 redo:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
328 len = ffm->packet_end - ffm->packet_ptr;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
329 if (len > size)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
330 len = size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
331 if (len == 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
332 if (url_ftell(pb) == ffm->file_size)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
333 url_fseek(pb, ffm->packet_size, SEEK_SET);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
334 retry_read:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
335 get_be16(pb); /* PACKET_ID */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
336 fill_size = get_be16(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
337 ffm->pts = get_be64(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
338 frame_offset = get_be16(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
339 get_buffer(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
340 ffm->packet_end = ffm->packet + (ffm->packet_size - FFM_HEADER_SIZE - fill_size);
318
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
341 if (ffm->packet_end < ffm->packet)
537
558a093b04db do not call (av_)abort()
michael
parents: 502
diff changeset
342 return -1;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
343 /* if first packet or resynchronization packet, we must
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
344 handle it specifically */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
345 if (ffm->first_packet || (frame_offset & 0x8000)) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
346 if (!frame_offset) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
347 /* This packet has no frame headers in it */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
348 if (url_ftell(pb) >= ffm->packet_size * 3) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
349 url_fseek(pb, -ffm->packet_size * 2, SEEK_CUR);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
350 goto retry_read;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
351 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
352 /* This is bad, we cannot find a valid frame header */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
353 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
354 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
355 ffm->first_packet = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
356 if ((frame_offset & 0x7ffff) < FFM_HEADER_SIZE)
537
558a093b04db do not call (av_)abort()
michael
parents: 502
diff changeset
357 return -1;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
358 ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
359 if (!first)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
360 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
361 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
362 ffm->packet_ptr = ffm->packet;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
363 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
364 goto redo;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
365 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
366 memcpy(buf, ffm->packet_ptr, len);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
367 buf += len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
368 ffm->packet_ptr += len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
369 size -= len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
370 first = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
371 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
372 return size1 - size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
373 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
374
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
375
318
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
376 static void adjust_write_index(AVFormatContext *s)
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
377 {
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
378 FFMContext *ffm = s->priv_data;
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
379 ByteIOContext *pb = &s->pb;
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
380 int64_t pts;
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
381 //offset_t orig_write_index = ffm->write_index;
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
382 offset_t pos_min, pos_max;
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
383 int64_t pts_start;
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
384 offset_t ptr = url_ftell(pb);
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
385
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
386
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
387 pos_min = 0;
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
388 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
389
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
390 pts_start = get_pts(s, pos_min);
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
391
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
392 pts = get_pts(s, pos_max);
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
393
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
394 if (pts - 100000 > pts_start)
390
3a40642dc4df adjust_write_index() fix by ("Curi Fabio Eduardo (SFL)" <curif at TELEFONICA dot COM dot AR>)
michael
parents: 318
diff changeset
395 goto end;
318
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
396
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
397 ffm->write_index = FFM_PACKET_SIZE;
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
398
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
399 pts_start = get_pts(s, pos_min);
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
400
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
401 pts = get_pts(s, pos_max);
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
402
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
403 if (pts - 100000 <= pts_start) {
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
404 while (1) {
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
405 offset_t newpos;
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
406 int64_t newpts;
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
407
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
408 newpos = ((pos_max + pos_min) / (2 * FFM_PACKET_SIZE)) * FFM_PACKET_SIZE;
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
409
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
410 if (newpos == pos_min)
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
411 break;
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
412
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
413 newpts = get_pts(s, newpos);
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
414
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
415 if (newpts - 100000 <= pts) {
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
416 pos_max = newpos;
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
417 pts = newpts;
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
418 } else {
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
419 pos_min = newpos;
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
420 }
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
421 }
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
422 ffm->write_index += pos_max;
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
423 }
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
424
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
425 //printf("Adjusted write index from %lld to %lld: pts=%0.6f\n", orig_write_index, ffm->write_index, pts / 1000000.);
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
426 //printf("pts range %0.6f - %0.6f\n", get_pts(s, 0) / 1000000. , get_pts(s, ffm->file_size - 2 * FFM_PACKET_SIZE) / 1000000. );
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
427
390
3a40642dc4df adjust_write_index() fix by ("Curi Fabio Eduardo (SFL)" <curif at TELEFONICA dot COM dot AR>)
michael
parents: 318
diff changeset
428 end:
318
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
429 url_fseek(pb, ptr, SEEK_SET);
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
430 }
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
431
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
432
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
433 static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
434 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
435 FFMContext *ffm = s->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
436 AVStream *st;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
437 FFMStream *fst;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
438 ByteIOContext *pb = &s->pb;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
439 AVCodecContext *codec;
187
2fa5e94ba716 add av_new_stream() usage
bellard
parents: 85
diff changeset
440 int i, nb_streams;
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 34
diff changeset
441 uint32_t tag;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
442
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
443 /* header */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
444 tag = get_le32(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
445 if (tag != MKTAG('F', 'F', 'M', '1'))
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
446 goto fail;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
447 ffm->packet_size = get_be32(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
448 if (ffm->packet_size != FFM_PACKET_SIZE)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
449 goto fail;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
450 ffm->write_index = get_be64(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
451 /* get also filesize */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
452 if (!url_is_streamed(pb)) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
453 ffm->file_size = url_filesize(url_fileno(pb));
318
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
454 adjust_write_index(s);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
455 } else {
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 34
diff changeset
456 ffm->file_size = (uint64_t_C(1) << 63) - 1;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
457 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
458
187
2fa5e94ba716 add av_new_stream() usage
bellard
parents: 85
diff changeset
459 nb_streams = get_be32(pb);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
460 get_be32(pb); /* total bitrate */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
461 /* read each stream */
187
2fa5e94ba716 add av_new_stream() usage
bellard
parents: 85
diff changeset
462 for(i=0;i<nb_streams;i++) {
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
463 char rc_eq_buf[128];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
464
187
2fa5e94ba716 add av_new_stream() usage
bellard
parents: 85
diff changeset
465 st = av_new_stream(s, 0);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
466 if (!st)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
467 goto fail;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
468 fst = av_mallocz(sizeof(FFMStream));
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
469 if (!fst)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
470 goto fail;
468
60f897e8dd2d pass AVPacket into av_write_frame()
michael
parents: 390
diff changeset
471
60f897e8dd2d pass AVPacket into av_write_frame()
michael
parents: 390
diff changeset
472 av_set_pts_info(st, 64, 1, 1000000);
60f897e8dd2d pass AVPacket into av_write_frame()
michael
parents: 390
diff changeset
473
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
474 st->priv_data = fst;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
475
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
476 codec = &st->codec;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
477 /* generic info */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
478 st->codec.codec_id = get_be32(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
479 st->codec.codec_type = get_byte(pb); /* codec_type */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
480 codec->bit_rate = get_be32(pb);
5
39c4c4336486 cleanup
michaelni
parents: 0
diff changeset
481 st->quality = get_be32(pb);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
482 codec->flags = get_be32(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
483 /* specific info */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
484 switch(codec->codec_type) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
485 case CODEC_TYPE_VIDEO:
85
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 79
diff changeset
486 codec->frame_rate_base = get_be32(pb);
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 79
diff changeset
487 codec->frame_rate = get_be32(pb);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
488 codec->width = get_be16(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
489 codec->height = get_be16(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
490 codec->gop_size = get_be16(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
491 codec->qmin = get_byte(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
492 codec->qmax = get_byte(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
493 codec->max_qdiff = get_byte(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
494 codec->qcompress = get_be16(pb) / 10000.0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
495 codec->qblur = get_be16(pb) / 10000.0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
496 codec->bit_rate_tolerance = get_be32(pb);
34
f17e285df237 use av_strdup()
bellard
parents: 7
diff changeset
497 codec->rc_eq = av_strdup(get_strz(pb, rc_eq_buf, sizeof(rc_eq_buf)));
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
498 codec->rc_max_rate = get_be32(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
499 codec->rc_min_rate = get_be32(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
500 codec->rc_buffer_size = get_be32(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
501 codec->i_quant_factor = get_be64_double(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
502 codec->b_quant_factor = get_be64_double(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
503 codec->i_quant_offset = get_be64_double(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
504 codec->b_quant_offset = get_be64_double(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
505 codec->dct_algo = get_be32(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
506 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
507 case CODEC_TYPE_AUDIO:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
508 codec->sample_rate = get_be32(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
509 codec->channels = get_le16(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
510 codec->frame_size = get_le16(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
511 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
512 default:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
513 goto fail;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
514 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
515
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
516 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
517
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
518 /* get until end of block reached */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
519 while ((url_ftell(pb) % ffm->packet_size) != 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
520 get_byte(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
521
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
522 /* init packet demux */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
523 ffm->packet_ptr = ffm->packet;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
524 ffm->packet_end = ffm->packet;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
525 ffm->frame_offset = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
526 ffm->pts = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
527 ffm->read_state = READ_HEADER;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
528 ffm->first_packet = 1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
529 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
530 fail:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
531 for(i=0;i<s->nb_streams;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
532 st = s->streams[i];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
533 if (st) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
534 av_freep(&st->priv_data);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
535 av_free(st);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
536 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
537 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
538 return -1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
539 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
540
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
541 /* return < 0 if eof */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
542 static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
543 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
544 int size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
545 FFMContext *ffm = s->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
546 int duration;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
547
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
548 switch(ffm->read_state) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
549 case READ_HEADER:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
550 if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE)) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
551 return -EAGAIN;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
552 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
553 #if 0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
554 printf("pos=%08Lx spos=%Lx, write_index=%Lx size=%Lx\n",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
555 url_ftell(&s->pb), s->pb.pos, ffm->write_index, ffm->file_size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
556 #endif
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
557 if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
558 FRAME_HEADER_SIZE)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
559 return -EAGAIN;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
560 #if 0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
561 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
562 int i;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
563 for(i=0;i<FRAME_HEADER_SIZE;i++)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
564 printf("%02x ", ffm->header[i]);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
565 printf("\n");
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
566 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
567 #endif
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
568 ffm->read_state = READ_DATA;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
569 /* fall thru */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
570 case READ_DATA:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
571 size = (ffm->header[2] << 16) | (ffm->header[3] << 8) | ffm->header[4];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
572 if (!ffm_is_avail_data(s, size)) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
573 return -EAGAIN;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
574 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
575
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
576 duration = (ffm->header[5] << 16) | (ffm->header[6] << 8) | ffm->header[7];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
577
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
578 av_new_packet(pkt, size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
579 pkt->stream_index = ffm->header[0];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
580 if (ffm->header[1] & FLAG_KEY_FRAME)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
581 pkt->flags |= PKT_FLAG_KEY;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
582
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
583 ffm->read_state = READ_HEADER;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
584 if (ffm_read_data(s, pkt->data, size, 0) != size) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
585 /* bad case: desynchronized packet. we cancel all the packet loading */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
586 av_free_packet(pkt);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
587 return -EAGAIN;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
588 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
589 pkt->pts = ffm->pts;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
590 pkt->duration = duration;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
591 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
592 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
593 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
594 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
595
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
596 //#define DEBUG_SEEK
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
597
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
598 /* pos is between 0 and file_size - FFM_PACKET_SIZE. It is translated
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
599 by the write position inside this function */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
600 static void ffm_seek1(AVFormatContext *s, offset_t pos1)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
601 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
602 FFMContext *ffm = s->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
603 ByteIOContext *pb = &s->pb;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
604 offset_t pos;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
605
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
606 pos = pos1 + ffm->write_index;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
607 if (pos >= ffm->file_size)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
608 pos -= (ffm->file_size - FFM_PACKET_SIZE);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
609 #ifdef DEBUG_SEEK
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
610 printf("seek to %Lx -> %Lx\n", pos1, pos);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
611 #endif
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
612 url_fseek(pb, pos, SEEK_SET);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
613 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
614
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 34
diff changeset
615 static int64_t get_pts(AVFormatContext *s, offset_t pos)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
616 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
617 ByteIOContext *pb = &s->pb;
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 34
diff changeset
618 int64_t pts;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
619
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
620 ffm_seek1(s, pos);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
621 url_fskip(pb, 4);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
622 pts = get_be64(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
623 #ifdef DEBUG_SEEK
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
624 printf("pts=%0.6f\n", pts / 1000000.0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
625 #endif
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
626 return pts;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
627 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
628
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
629 /* seek to a given time in the file. The file read pointer is
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
630 positionned at or before pts. XXX: the following code is quite
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
631 approximative */
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 34
diff changeset
632 static int ffm_seek(AVFormatContext *s, int64_t wanted_pts)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
633 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
634 FFMContext *ffm = s->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
635 offset_t pos_min, pos_max, pos;
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 34
diff changeset
636 int64_t pts_min, pts_max, pts;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
637 double pos1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
638
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
639 #ifdef DEBUG_SEEK
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
640 printf("wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
641 #endif
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
642 /* find the position using linear interpolation (better than
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
643 dichotomy in typical cases) */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
644 pos_min = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
645 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
646 while (pos_min <= pos_max) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
647 pts_min = get_pts(s, pos_min);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
648 pts_max = get_pts(s, pos_max);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
649 /* linear interpolation */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
650 pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
651 (double)(pts_max - pts_min);
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 34
diff changeset
652 pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
653 if (pos <= pos_min)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
654 pos = pos_min;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
655 else if (pos >= pos_max)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
656 pos = pos_max;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
657 pts = get_pts(s, pos);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
658 /* check if we are lucky */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
659 if (pts == wanted_pts) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
660 goto found;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
661 } else if (pts > wanted_pts) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
662 pos_max = pos - FFM_PACKET_SIZE;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
663 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
664 pos_min = pos + FFM_PACKET_SIZE;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
665 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
666 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
667 pos = pos_min;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
668 if (pos > 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
669 pos -= FFM_PACKET_SIZE;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
670 found:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
671 ffm_seek1(s, pos);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
672 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
673 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
674
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
675 offset_t ffm_read_write_index(int fd)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
676 {
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 34
diff changeset
677 uint8_t buf[8];
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
678 offset_t pos;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
679 int i;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
680
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
681 lseek(fd, 8, SEEK_SET);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
682 read(fd, buf, 8);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
683 pos = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
684 for(i=0;i<8;i++)
187
2fa5e94ba716 add av_new_stream() usage
bellard
parents: 85
diff changeset
685 pos |= (int64_t)buf[i] << (56 - i * 8);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
686 return pos;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
687 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
688
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
689 void ffm_write_write_index(int fd, offset_t pos)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
690 {
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 34
diff changeset
691 uint8_t buf[8];
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
692 int i;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
693
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
694 for(i=0;i<8;i++)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
695 buf[i] = (pos >> (56 - i * 8)) & 0xff;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
696 lseek(fd, 8, SEEK_SET);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
697 write(fd, buf, 8);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
698 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
699
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
700 void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
701 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
702 FFMContext *ffm = s->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
703 ffm->write_index = pos;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
704 ffm->file_size = file_size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
705 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
706
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
707 static int ffm_read_close(AVFormatContext *s)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
708 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
709 AVStream *st;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
710 int i;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
711
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
712 for(i=0;i<s->nb_streams;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
713 st = s->streams[i];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
714 av_freep(&st->priv_data);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
715 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
716 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
717 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
718
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
719 static int ffm_probe(AVProbeData *p)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
720 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
721 if (p->buf_size >= 4 &&
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
722 p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
723 p->buf[3] == '1')
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
724 return AVPROBE_SCORE_MAX + 1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
725 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
726 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
727
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
728 static AVInputFormat ffm_iformat = {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
729 "ffm",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
730 "ffm format",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
731 sizeof(FFMContext),
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
732 ffm_probe,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
733 ffm_read_header,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
734 ffm_read_packet,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
735 ffm_read_close,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
736 ffm_seek,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
737 };
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
738
277
a313e1080322 disable encoders where appropriate (patch courtesy of BERO
melanson
parents: 241
diff changeset
739 #ifdef CONFIG_ENCODERS
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
740 static AVOutputFormat ffm_oformat = {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
741 "ffm",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
742 "ffm format",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
743 "",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
744 "ffm",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
745 sizeof(FFMContext),
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
746 /* not really used */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
747 CODEC_ID_MP2,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
748 CODEC_ID_MPEG1VIDEO,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
749 ffm_write_header,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
750 ffm_write_packet,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
751 ffm_write_trailer,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
752 };
277
a313e1080322 disable encoders where appropriate (patch courtesy of BERO
melanson
parents: 241
diff changeset
753 #endif //CONFIG_ENCODERS
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
754
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
755 int ffm_init(void)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
756 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
757 av_register_input_format(&ffm_iformat);
277
a313e1080322 disable encoders where appropriate (patch courtesy of BERO
melanson
parents: 241
diff changeset
758 #ifdef CONFIG_ENCODERS
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
759 av_register_output_format(&ffm_oformat);
277
a313e1080322 disable encoders where appropriate (patch courtesy of BERO
melanson
parents: 241
diff changeset
760 #endif //CONFIG_ENCODERS
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
761 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
762 }