annotate ffmdec.c @ 3348:4d492fccf79b libavformat

split ffm de/muxer
author bcoudurier
date Mon, 26 May 2008 03:44:31 +0000
parents ffm.c@a6449316fad1
children e092bf9b744f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1 /*
3348
4d492fccf79b split ffm de/muxer
bcoudurier
parents: 3347
diff changeset
2 * FFM (ffserver live feed) demuxer
0
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 *
1358
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1169
diff changeset
5 * This file is part of FFmpeg.
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1169
diff changeset
6 *
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1169
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
1358
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1169
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
11 *
1358
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1169
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
15 * Lesser General Public License for more details.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
16 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
1358
0899bfe4105c Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents: 1169
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
896
edbe5c3717f9 Update licensing information: The FSF changed postal address.
diego
parents: 887
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
20 */
3348
4d492fccf79b split ffm de/muxer
bcoudurier
parents: 3347
diff changeset
21
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
22 #include "avformat.h"
3348
4d492fccf79b split ffm de/muxer
bcoudurier
parents: 3347
diff changeset
23 #include "ffm.h"
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
24 #include <unistd.h>
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
25
318
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
26 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
27
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
28 static int ffm_is_avail_data(AVFormatContext *s, int size)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
29 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
30 FFMContext *ffm = s->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
31 offset_t pos, avail_size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
32 int len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
33
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
34 len = ffm->packet_end - ffm->packet_ptr;
3347
a6449316fad1 reenable test, data is available in this case
bcoudurier
parents: 3312
diff changeset
35 if (size <= len)
a6449316fad1 reenable test, data is available in this case
bcoudurier
parents: 3312
diff changeset
36 return 1;
2771
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2222
diff changeset
37 pos = url_ftell(s->pb);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
38 if (pos == ffm->write_index) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
39 /* exactly at the end of stream */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
40 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
41 } else if (pos < ffm->write_index) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
42 avail_size = ffm->write_index - pos;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
43 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
44 avail_size = (ffm->file_size - pos) + (ffm->write_index - FFM_PACKET_SIZE);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
45 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
46 avail_size = (avail_size / ffm->packet_size) * (ffm->packet_size - FFM_HEADER_SIZE) + len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
47 if (size <= avail_size)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
48 return 1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
49 else
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
50 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
51 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
52
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
53 /* first is true if we read the frame header */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
54 static int ffm_read_data(AVFormatContext *s,
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 34
diff changeset
55 uint8_t *buf, int size, int first)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
56 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
57 FFMContext *ffm = s->priv_data;
2771
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2222
diff changeset
58 ByteIOContext *pb = s->pb;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
59 int len, fill_size, size1, frame_offset;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
60
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
61 size1 = size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
62 while (size > 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
63 redo:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
64 len = ffm->packet_end - ffm->packet_ptr;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
65 if (len > size)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
66 len = size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
67 if (len == 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
68 if (url_ftell(pb) == ffm->file_size)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
69 url_fseek(pb, ffm->packet_size, SEEK_SET);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
70 retry_read:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
71 get_be16(pb); /* PACKET_ID */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
72 fill_size = get_be16(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
73 ffm->pts = get_be64(pb);
901
c1a07d63a66d pts fix by (Bryan Mayland / bmayland O leoninedev o com)
michael
parents: 896
diff changeset
74 ffm->first_frame_in_packet = 1;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
75 frame_offset = get_be16(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
76 get_buffer(pb, ffm->packet, ffm->packet_size - FFM_HEADER_SIZE);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
77 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
78 if (ffm->packet_end < ffm->packet)
537
558a093b04db do not call (av_)abort()
michael
parents: 502
diff changeset
79 return -1;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
80 /* if first packet or resynchronization packet, we must
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
81 handle it specifically */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
82 if (ffm->first_packet || (frame_offset & 0x8000)) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
83 if (!frame_offset) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
84 /* This packet has no frame headers in it */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
85 if (url_ftell(pb) >= ffm->packet_size * 3) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
86 url_fseek(pb, -ffm->packet_size * 2, SEEK_CUR);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
87 goto retry_read;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
88 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
89 /* This is bad, we cannot find a valid frame header */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
90 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
91 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
92 ffm->first_packet = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
93 if ((frame_offset & 0x7ffff) < FFM_HEADER_SIZE)
537
558a093b04db do not call (av_)abort()
michael
parents: 502
diff changeset
94 return -1;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
95 ffm->packet_ptr = ffm->packet + (frame_offset & 0x7fff) - FFM_HEADER_SIZE;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
96 if (!first)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
97 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
98 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
99 ffm->packet_ptr = ffm->packet;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
100 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
101 goto redo;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
102 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
103 memcpy(buf, ffm->packet_ptr, len);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
104 buf += len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
105 ffm->packet_ptr += len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
106 size -= len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
107 first = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
108 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
109 return size1 - size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
110 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
111
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
112
318
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
113 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
114 {
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
115 FFMContext *ffm = s->priv_data;
2771
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2222
diff changeset
116 ByteIOContext *pb = s->pb;
318
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
117 int64_t pts;
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
118 //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
119 offset_t pos_min, pos_max;
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
120 int64_t pts_start;
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
121 offset_t ptr = url_ftell(pb);
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
122
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
123
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
124 pos_min = 0;
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
125 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
126
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
127 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
128
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
129 pts = get_pts(s, pos_max);
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
130
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 862
diff changeset
131 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
132 goto end;
318
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
133
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
134 ffm->write_index = FFM_PACKET_SIZE;
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
135
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
136 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
137
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
138 pts = get_pts(s, pos_max);
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
139
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
140 if (pts - 100000 <= pts_start) {
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
141 while (1) {
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
142 offset_t newpos;
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
143 int64_t newpts;
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
144
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
145 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
146
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
147 if (newpos == pos_min)
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
148 break;
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
149
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
150 newpts = get_pts(s, newpos);
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
151
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
152 if (newpts - 100000 <= pts) {
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
153 pos_max = newpos;
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
154 pts = newpts;
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
155 } else {
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
156 pos_min = newpos;
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
157 }
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
158 }
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
159 ffm->write_index += pos_max;
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
160 }
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
161
1443
404048ea11bc Replace most of the %lld and %llx by their (cleaner) PRI*64 counterparts.
diego
parents: 1415
diff changeset
162 //printf("Adjusted write index from %"PRId64" to %"PRId64": pts=%0.6f\n", orig_write_index, ffm->write_index, pts / 1000000.);
318
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
163 //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
164
390
3a40642dc4df adjust_write_index() fix by ("Curi Fabio Eduardo (SFL)" <curif at TELEFONICA dot COM dot AR>)
michael
parents: 318
diff changeset
165 end:
318
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
166 url_fseek(pb, ptr, SEEK_SET);
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
167 }
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
168
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
169
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
170 static int ffm_read_header(AVFormatContext *s, AVFormatParameters *ap)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
171 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
172 FFMContext *ffm = s->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
173 AVStream *st;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
174 FFMStream *fst;
2771
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2222
diff changeset
175 ByteIOContext *pb = s->pb;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
176 AVCodecContext *codec;
187
2fa5e94ba716 add av_new_stream() usage
bellard
parents: 85
diff changeset
177 int i, nb_streams;
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 34
diff changeset
178 uint32_t tag;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
179
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
180 /* header */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
181 tag = get_le32(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
182 if (tag != MKTAG('F', 'F', 'M', '1'))
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
183 goto fail;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
184 ffm->packet_size = get_be32(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
185 if (ffm->packet_size != FFM_PACKET_SIZE)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
186 goto fail;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
187 ffm->write_index = get_be64(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
188 /* get also filesize */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
189 if (!url_is_streamed(pb)) {
764
cdb845a57ae4 drop most url_fileno() calls (allows to use ByteIOContext directly in caller apps instead of URLProtocol)
aurel
parents: 751
diff changeset
190 ffm->file_size = url_fsize(pb);
318
54e915169d48 Add more resilience in reading ffm files. In particular, don't assume
philipjsg
parents: 277
diff changeset
191 adjust_write_index(s);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
192 } else {
1556
65b7b3ff4ed7 use the standard INT64_C() macro for 64-bit constants
mru
parents: 1443
diff changeset
193 ffm->file_size = (UINT64_C(1) << 63) - 1;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
194 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
195
187
2fa5e94ba716 add av_new_stream() usage
bellard
parents: 85
diff changeset
196 nb_streams = get_be32(pb);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
197 get_be32(pb); /* total bitrate */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
198 /* read each stream */
187
2fa5e94ba716 add av_new_stream() usage
bellard
parents: 85
diff changeset
199 for(i=0;i<nb_streams;i++) {
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
200 char rc_eq_buf[128];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
201
187
2fa5e94ba716 add av_new_stream() usage
bellard
parents: 85
diff changeset
202 st = av_new_stream(s, 0);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
203 if (!st)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
204 goto fail;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
205 fst = av_mallocz(sizeof(FFMStream));
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
206 if (!fst)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
207 goto fail;
862
aa0abab5e320 fix feed read_header, avoid using put_tag in write_header, to be consistent with read_header, also some minor cosmetics
alex
parents: 858
diff changeset
208 s->streams[i] = st;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 862
diff changeset
209
468
60f897e8dd2d pass AVPacket into av_write_frame()
michael
parents: 390
diff changeset
210 av_set_pts_info(st, 64, 1, 1000000);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 862
diff changeset
211
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
212 st->priv_data = fst;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
213
820
feca73904e67 changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents: 775
diff changeset
214 codec = st->codec;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
215 /* generic info */
862
aa0abab5e320 fix feed read_header, avoid using put_tag in write_header, to be consistent with read_header, also some minor cosmetics
alex
parents: 858
diff changeset
216 codec->codec_id = get_be32(pb);
aa0abab5e320 fix feed read_header, avoid using put_tag in write_header, to be consistent with read_header, also some minor cosmetics
alex
parents: 858
diff changeset
217 codec->codec_type = get_byte(pb); /* codec_type */
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
218 codec->bit_rate = get_be32(pb);
862
aa0abab5e320 fix feed read_header, avoid using put_tag in write_header, to be consistent with read_header, also some minor cosmetics
alex
parents: 858
diff changeset
219 st->quality = get_be32(pb);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
220 codec->flags = get_be32(pb);
744
da5b3b9e898e Add in many fields that have been added to the Codec structure. This means
philipjsg
parents: 743
diff changeset
221 codec->flags2 = get_be32(pb);
da5b3b9e898e Add in many fields that have been added to the Codec structure. This means
philipjsg
parents: 743
diff changeset
222 codec->debug = get_be32(pb);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
223 /* specific info */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
224 switch(codec->codec_type) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
225 case CODEC_TYPE_VIDEO:
743
af4e24d6310c switch to native time bases
michael
parents: 683
diff changeset
226 codec->time_base.num = get_be32(pb);
af4e24d6310c switch to native time bases
michael
parents: 683
diff changeset
227 codec->time_base.den = get_be32(pb);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
228 codec->width = get_be16(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
229 codec->height = get_be16(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
230 codec->gop_size = get_be16(pb);
744
da5b3b9e898e Add in many fields that have been added to the Codec structure. This means
philipjsg
parents: 743
diff changeset
231 codec->pix_fmt = get_be32(pb);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
232 codec->qmin = get_byte(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
233 codec->qmax = get_byte(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
234 codec->max_qdiff = get_byte(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
235 codec->qcompress = get_be16(pb) / 10000.0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
236 codec->qblur = get_be16(pb) / 10000.0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
237 codec->bit_rate_tolerance = get_be32(pb);
34
f17e285df237 use av_strdup()
bellard
parents: 7
diff changeset
238 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
239 codec->rc_max_rate = get_be32(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
240 codec->rc_min_rate = get_be32(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
241 codec->rc_buffer_size = get_be32(pb);
823
e8b4454b997d remove non portable get/put_be64_double()
michael
parents: 820
diff changeset
242 codec->i_quant_factor = av_int2dbl(get_be64(pb));
e8b4454b997d remove non portable get/put_be64_double()
michael
parents: 820
diff changeset
243 codec->b_quant_factor = av_int2dbl(get_be64(pb));
e8b4454b997d remove non portable get/put_be64_double()
michael
parents: 820
diff changeset
244 codec->i_quant_offset = av_int2dbl(get_be64(pb));
e8b4454b997d remove non portable get/put_be64_double()
michael
parents: 820
diff changeset
245 codec->b_quant_offset = av_int2dbl(get_be64(pb));
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
246 codec->dct_algo = get_be32(pb);
744
da5b3b9e898e Add in many fields that have been added to the Codec structure. This means
philipjsg
parents: 743
diff changeset
247 codec->strict_std_compliance = get_be32(pb);
da5b3b9e898e Add in many fields that have been added to the Codec structure. This means
philipjsg
parents: 743
diff changeset
248 codec->max_b_frames = get_be32(pb);
da5b3b9e898e Add in many fields that have been added to the Codec structure. This means
philipjsg
parents: 743
diff changeset
249 codec->luma_elim_threshold = get_be32(pb);
da5b3b9e898e Add in many fields that have been added to the Codec structure. This means
philipjsg
parents: 743
diff changeset
250 codec->chroma_elim_threshold = get_be32(pb);
da5b3b9e898e Add in many fields that have been added to the Codec structure. This means
philipjsg
parents: 743
diff changeset
251 codec->mpeg_quant = get_be32(pb);
da5b3b9e898e Add in many fields that have been added to the Codec structure. This means
philipjsg
parents: 743
diff changeset
252 codec->intra_dc_precision = get_be32(pb);
da5b3b9e898e Add in many fields that have been added to the Codec structure. This means
philipjsg
parents: 743
diff changeset
253 codec->me_method = get_be32(pb);
da5b3b9e898e Add in many fields that have been added to the Codec structure. This means
philipjsg
parents: 743
diff changeset
254 codec->mb_decision = get_be32(pb);
da5b3b9e898e Add in many fields that have been added to the Codec structure. This means
philipjsg
parents: 743
diff changeset
255 codec->nsse_weight = get_be32(pb);
da5b3b9e898e Add in many fields that have been added to the Codec structure. This means
philipjsg
parents: 743
diff changeset
256 codec->frame_skip_cmp = get_be32(pb);
823
e8b4454b997d remove non portable get/put_be64_double()
michael
parents: 820
diff changeset
257 codec->rc_buffer_aggressivity = av_int2dbl(get_be64(pb));
1809
491581a2b9a7 codec_tag settable via VideoTag, and transmit codec_tag in ffm
alex
parents: 1787
diff changeset
258 codec->codec_tag = get_be32(pb);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
259 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
260 case CODEC_TYPE_AUDIO:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
261 codec->sample_rate = get_be32(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
262 codec->channels = get_le16(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
263 codec->frame_size = get_le16(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
264 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
265 default:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
266 goto fail;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
267 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
268
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
269 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
270
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
271 /* get until end of block reached */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
272 while ((url_ftell(pb) % ffm->packet_size) != 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
273 get_byte(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
274
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
275 /* init packet demux */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
276 ffm->packet_ptr = ffm->packet;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
277 ffm->packet_end = ffm->packet;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
278 ffm->frame_offset = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
279 ffm->pts = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
280 ffm->read_state = READ_HEADER;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
281 ffm->first_packet = 1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
282 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
283 fail:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
284 for(i=0;i<s->nb_streams;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
285 st = s->streams[i];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
286 if (st) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
287 av_freep(&st->priv_data);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
288 av_free(st);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
289 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
290 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
291 return -1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
292 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
293
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
294 /* return < 0 if eof */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
295 static int ffm_read_packet(AVFormatContext *s, AVPacket *pkt)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
296 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
297 int size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
298 FFMContext *ffm = s->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
299 int duration;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
300
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
301 switch(ffm->read_state) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
302 case READ_HEADER:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
303 if (!ffm_is_avail_data(s, FRAME_HEADER_SIZE)) {
1787
eb16c64144ee This fixes error handling for BeOS, removing the need for some ifdefs.
mmu_man
parents: 1556
diff changeset
304 return AVERROR(EAGAIN);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
305 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
306 #if 0
1443
404048ea11bc Replace most of the %lld and %llx by their (cleaner) PRI*64 counterparts.
diego
parents: 1415
diff changeset
307 printf("pos=%08"PRIx64" spos=%"PRIx64", write_index=%"PRIx64" size=%"PRIx64"\n",
2771
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2222
diff changeset
308 url_ftell(s->pb), s->pb.pos, ffm->write_index, ffm->file_size);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
309 #endif
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 862
diff changeset
310 if (ffm_read_data(s, ffm->header, FRAME_HEADER_SIZE, 1) !=
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
311 FRAME_HEADER_SIZE)
1787
eb16c64144ee This fixes error handling for BeOS, removing the need for some ifdefs.
mmu_man
parents: 1556
diff changeset
312 return AVERROR(EAGAIN);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
313 #if 0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
314 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
315 int i;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
316 for(i=0;i<FRAME_HEADER_SIZE;i++)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
317 printf("%02x ", ffm->header[i]);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
318 printf("\n");
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
319 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
320 #endif
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
321 ffm->read_state = READ_DATA;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
322 /* fall thru */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
323 case READ_DATA:
2222
3427d0c63a32 Use AV_RB* macros where appropriate.
diego
parents: 2001
diff changeset
324 size = AV_RB24(ffm->header + 2);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
325 if (!ffm_is_avail_data(s, size)) {
1787
eb16c64144ee This fixes error handling for BeOS, removing the need for some ifdefs.
mmu_man
parents: 1556
diff changeset
326 return AVERROR(EAGAIN);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
327 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
328
2222
3427d0c63a32 Use AV_RB* macros where appropriate.
diego
parents: 2001
diff changeset
329 duration = AV_RB24(ffm->header + 5);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
330
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
331 av_new_packet(pkt, size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
332 pkt->stream_index = ffm->header[0];
3307
e4ff879325c0 check pkt stream index before returning packet, prevent segfault
bcoudurier
parents: 2915
diff changeset
333 if ((unsigned)pkt->stream_index >= s->nb_streams) {
e4ff879325c0 check pkt stream index before returning packet, prevent segfault
bcoudurier
parents: 2915
diff changeset
334 av_log(s, AV_LOG_ERROR, "invalid stream index %d\n", pkt->stream_index);
e4ff879325c0 check pkt stream index before returning packet, prevent segfault
bcoudurier
parents: 2915
diff changeset
335 av_free_packet(pkt);
e4ff879325c0 check pkt stream index before returning packet, prevent segfault
bcoudurier
parents: 2915
diff changeset
336 ffm->read_state = READ_HEADER;
e4ff879325c0 check pkt stream index before returning packet, prevent segfault
bcoudurier
parents: 2915
diff changeset
337 return AVERROR(EAGAIN);
e4ff879325c0 check pkt stream index before returning packet, prevent segfault
bcoudurier
parents: 2915
diff changeset
338 }
2771
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2222
diff changeset
339 pkt->pos = url_ftell(s->pb);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
340 if (ffm->header[1] & FLAG_KEY_FRAME)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
341 pkt->flags |= PKT_FLAG_KEY;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
342
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
343 ffm->read_state = READ_HEADER;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
344 if (ffm_read_data(s, pkt->data, size, 0) != size) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
345 /* bad case: desynchronized packet. we cancel all the packet loading */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
346 av_free_packet(pkt);
1787
eb16c64144ee This fixes error handling for BeOS, removing the need for some ifdefs.
mmu_man
parents: 1556
diff changeset
347 return AVERROR(EAGAIN);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
348 }
901
c1a07d63a66d pts fix by (Bryan Mayland / bmayland O leoninedev o com)
michael
parents: 896
diff changeset
349 if (ffm->first_frame_in_packet)
c1a07d63a66d pts fix by (Bryan Mayland / bmayland O leoninedev o com)
michael
parents: 896
diff changeset
350 {
c1a07d63a66d pts fix by (Bryan Mayland / bmayland O leoninedev o com)
michael
parents: 896
diff changeset
351 pkt->pts = ffm->pts;
c1a07d63a66d pts fix by (Bryan Mayland / bmayland O leoninedev o com)
michael
parents: 896
diff changeset
352 ffm->first_frame_in_packet = 0;
c1a07d63a66d pts fix by (Bryan Mayland / bmayland O leoninedev o com)
michael
parents: 896
diff changeset
353 }
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
354 pkt->duration = duration;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
355 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
356 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
357 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
358 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
359
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
360 //#define DEBUG_SEEK
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
361
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
362 /* pos is between 0 and file_size - FFM_PACKET_SIZE. It is translated
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
363 by the write position inside this function */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
364 static void ffm_seek1(AVFormatContext *s, offset_t pos1)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
365 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
366 FFMContext *ffm = s->priv_data;
2771
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2222
diff changeset
367 ByteIOContext *pb = s->pb;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
368 offset_t pos;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
369
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
370 pos = pos1 + ffm->write_index;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
371 if (pos >= ffm->file_size)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
372 pos -= (ffm->file_size - FFM_PACKET_SIZE);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
373 #ifdef DEBUG_SEEK
1443
404048ea11bc Replace most of the %lld and %llx by their (cleaner) PRI*64 counterparts.
diego
parents: 1415
diff changeset
374 printf("seek to %"PRIx64" -> %"PRIx64"\n", pos1, pos);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
375 #endif
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
376 url_fseek(pb, pos, SEEK_SET);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
377 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
378
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 34
diff changeset
379 static int64_t get_pts(AVFormatContext *s, offset_t pos)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
380 {
2771
d52c718e83f9 Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents: 2222
diff changeset
381 ByteIOContext *pb = s->pb;
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 34
diff changeset
382 int64_t pts;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
383
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
384 ffm_seek1(s, pos);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
385 url_fskip(pb, 4);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
386 pts = get_be64(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
387 #ifdef DEBUG_SEEK
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
388 printf("pts=%0.6f\n", pts / 1000000.0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
389 #endif
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
390 return pts;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
391 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
392
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
393 /* seek to a given time in the file. The file read pointer is
2915
75d64cb5ee6c Fix random typos.
diego
parents: 2771
diff changeset
394 positioned at or before pts. XXX: the following code is quite
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
395 approximative */
558
89bd76208427 100l (forgoten seeking functions)
michael
parents: 540
diff changeset
396 static int ffm_seek(AVFormatContext *s, int stream_index, int64_t wanted_pts, int flags)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
397 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
398 FFMContext *ffm = s->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
399 offset_t pos_min, pos_max, pos;
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 34
diff changeset
400 int64_t pts_min, pts_max, pts;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
401 double pos1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
402
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
403 #ifdef DEBUG_SEEK
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
404 printf("wanted_pts=%0.6f\n", wanted_pts / 1000000.0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
405 #endif
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
406 /* find the position using linear interpolation (better than
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
407 dichotomy in typical cases) */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
408 pos_min = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
409 pos_max = ffm->file_size - 2 * FFM_PACKET_SIZE;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
410 while (pos_min <= pos_max) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
411 pts_min = get_pts(s, pos_min);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
412 pts_max = get_pts(s, pos_max);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
413 /* linear interpolation */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
414 pos1 = (double)(pos_max - pos_min) * (double)(wanted_pts - pts_min) /
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
415 (double)(pts_max - pts_min);
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 34
diff changeset
416 pos = (((int64_t)pos1) / FFM_PACKET_SIZE) * FFM_PACKET_SIZE;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
417 if (pos <= pos_min)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
418 pos = pos_min;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
419 else if (pos >= pos_max)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
420 pos = pos_max;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
421 pts = get_pts(s, pos);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
422 /* check if we are lucky */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
423 if (pts == wanted_pts) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
424 goto found;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
425 } else if (pts > wanted_pts) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
426 pos_max = pos - FFM_PACKET_SIZE;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
427 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
428 pos_min = pos + FFM_PACKET_SIZE;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
429 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
430 }
558
89bd76208427 100l (forgoten seeking functions)
michael
parents: 540
diff changeset
431 pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
432 if (pos > 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
433 pos -= FFM_PACKET_SIZE;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
434 found:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
435 ffm_seek1(s, pos);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
436 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
437 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
438
905
dbc0145bbf11 Add --disable-protocols option to configure to disable I/O protocol from
diego
parents: 901
diff changeset
439 #ifdef CONFIG_FFSERVER
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
440 offset_t ffm_read_write_index(int fd)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
441 {
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 34
diff changeset
442 uint8_t buf[8];
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
443
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
444 lseek(fd, 8, SEEK_SET);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
445 read(fd, buf, 8);
2222
3427d0c63a32 Use AV_RB* macros where appropriate.
diego
parents: 2001
diff changeset
446 return AV_RB64(buf);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
447 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
448
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
449 void ffm_write_write_index(int fd, offset_t pos)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
450 {
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 34
diff changeset
451 uint8_t buf[8];
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
452 int i;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
453
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
454 for(i=0;i<8;i++)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
455 buf[i] = (pos >> (56 - i * 8)) & 0xff;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
456 lseek(fd, 8, SEEK_SET);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
457 write(fd, buf, 8);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
458 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
459
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
460 void ffm_set_write_index(AVFormatContext *s, offset_t pos, offset_t file_size)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
461 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
462 FFMContext *ffm = s->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
463 ffm->write_index = pos;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
464 ffm->file_size = file_size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
465 }
905
dbc0145bbf11 Add --disable-protocols option to configure to disable I/O protocol from
diego
parents: 901
diff changeset
466 #endif // CONFIG_FFSERVER
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
467
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
468 static int ffm_read_close(AVFormatContext *s)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
469 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
470 AVStream *st;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
471 int i;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
472
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
473 for(i=0;i<s->nb_streams;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
474 st = s->streams[i];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
475 av_freep(&st->priv_data);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
476 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
477 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
478 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
479
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
480 static int ffm_probe(AVProbeData *p)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
481 {
2001
1a3c9056982a allocate 32 extra bytes at the end of the probe buffer and remove most probe buf_size checks
michael
parents: 1809
diff changeset
482 if (
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 862
diff changeset
483 p->buf[0] == 'F' && p->buf[1] == 'F' && p->buf[2] == 'M' &&
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
484 p->buf[3] == '1')
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
485 return AVPROBE_SCORE_MAX + 1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
486 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
487 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
488
1169
d18cc9a1fd02 allow individual selection of muxers and demuxers
mru
parents: 1167
diff changeset
489 AVInputFormat ffm_demuxer = {
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
490 "ffm",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
491 "ffm format",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
492 sizeof(FFMContext),
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
493 ffm_probe,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
494 ffm_read_header,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
495 ffm_read_packet,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
496 ffm_read_close,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
497 ffm_seek,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
498 };