annotate utils.c @ 30:90fd30dd68b3 libavformat

grab device is in AVFormatParameter (at least better than global variable)
author bellard
date Thu, 23 Jan 2003 10:33:16 +0000
parents 86a085c19c7f
children cd0155c9022e
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 * Various utilities for ffmpeg system
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
3 * Copyright (c) 2000, 2001, 2002 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 <ctype.h>
22
65433f1b2549 os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents: 21
diff changeset
21 #ifdef CONFIG_WIN32
65433f1b2549 os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents: 21
diff changeset
22 #define strcasecmp _stricmp
65433f1b2549 os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents: 21
diff changeset
23 #include <sys/types.h>
65433f1b2549 os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents: 21
diff changeset
24 #include <sys/timeb.h>
65433f1b2549 os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents: 21
diff changeset
25 #elif defined(CONFIG_OS2)
65433f1b2549 os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents: 21
diff changeset
26 #include <string.h>
65433f1b2549 os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents: 21
diff changeset
27 #define strcasecmp stricmp
65433f1b2549 os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents: 21
diff changeset
28 #include <sys/time.h>
65433f1b2549 os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents: 21
diff changeset
29 #else
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
30 #include <unistd.h>
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
31 #include <fcntl.h>
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
32 #include <sys/time.h>
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
33 #endif
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
34 #include <time.h>
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
35
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
36 #ifndef HAVE_STRPTIME
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
37 #include "strptime.h"
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
38 #endif
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
39
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
40 AVInputFormat *first_iformat;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
41 AVOutputFormat *first_oformat;
20
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
42 AVImageFormat *first_image_format;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
43
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
44 void av_register_input_format(AVInputFormat *format)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
45 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
46 AVInputFormat **p;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
47 p = &first_iformat;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
48 while (*p != NULL) p = &(*p)->next;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
49 *p = format;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
50 format->next = NULL;
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 void av_register_output_format(AVOutputFormat *format)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
54 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
55 AVOutputFormat **p;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
56 p = &first_oformat;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
57 while (*p != NULL) p = &(*p)->next;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
58 *p = format;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
59 format->next = NULL;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
60 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
61
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
62 int match_ext(const char *filename, const char *extensions)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
63 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
64 const char *ext, *p;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
65 char ext1[32], *q;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
66
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
67 ext = strrchr(filename, '.');
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
68 if (ext) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
69 ext++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
70 p = extensions;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
71 for(;;) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
72 q = ext1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
73 while (*p != '\0' && *p != ',')
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
74 *q++ = *p++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
75 *q = '\0';
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
76 if (!strcasecmp(ext1, ext))
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
77 return 1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
78 if (*p == '\0')
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
79 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
80 p++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
81 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
82 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
83 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
84 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
85
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
86 AVOutputFormat *guess_format(const char *short_name, const char *filename,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
87 const char *mime_type)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
88 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
89 AVOutputFormat *fmt, *fmt_found;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
90 int score_max, score;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
91
20
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
92 /* specific test for image sequences */
21
3d2d2f8725f9 jpeg detection fix - fixed imagepipe output
bellard
parents: 20
diff changeset
93 if (!short_name && filename &&
3d2d2f8725f9 jpeg detection fix - fixed imagepipe output
bellard
parents: 20
diff changeset
94 filename_number_test(filename) >= 0 &&
3d2d2f8725f9 jpeg detection fix - fixed imagepipe output
bellard
parents: 20
diff changeset
95 guess_image_format(filename)) {
20
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
96 return guess_format("image", NULL, NULL);
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
97 }
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
98
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
99 /* find the proper file type */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
100 fmt_found = NULL;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
101 score_max = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
102 fmt = first_oformat;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
103 while (fmt != NULL) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
104 score = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
105 if (fmt->name && short_name && !strcmp(fmt->name, short_name))
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
106 score += 100;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
107 if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
108 score += 10;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
109 if (filename && fmt->extensions &&
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
110 match_ext(filename, fmt->extensions)) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
111 score += 5;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
112 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
113 if (score > score_max) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
114 score_max = score;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
115 fmt_found = fmt;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
116 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
117 fmt = fmt->next;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
118 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
119 return fmt_found;
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 AVOutputFormat *guess_stream_format(const char *short_name, const char *filename,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
123 const char *mime_type)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
124 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
125 AVOutputFormat *fmt = guess_format(short_name, filename, mime_type);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
126
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
127 if (fmt) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
128 AVOutputFormat *stream_fmt;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
129 char stream_format_name[64];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
130
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
131 snprintf(stream_format_name, sizeof(stream_format_name), "%s_stream", fmt->name);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
132 stream_fmt = guess_format(stream_format_name, NULL, NULL);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
133
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
134 if (stream_fmt)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
135 fmt = stream_fmt;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
136 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
137
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
138 return fmt;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
139 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
140
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
141 AVInputFormat *av_find_input_format(const char *short_name)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
142 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
143 AVInputFormat *fmt;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
144 for(fmt = first_iformat; fmt != NULL; fmt = fmt->next) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
145 if (!strcmp(fmt->name, short_name))
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
146 return fmt;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
147 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
148 return NULL;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
149 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
150
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
151 /* memory handling */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
152
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
153 /**
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
154 * Allocate the payload of a packet and intialized its fields to default values.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
155 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
156 * @param pkt packet
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
157 * @param size wanted payload size
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
158 * @return 0 if OK. AVERROR_xxx otherwise.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
159 */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
160 int av_new_packet(AVPacket *pkt, int size)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
161 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
162 int i;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
163 pkt->data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
164 if (!pkt->data)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
165 return AVERROR_NOMEM;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
166 pkt->size = size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
167 /* sane state */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
168 pkt->pts = AV_NOPTS_VALUE;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
169 pkt->stream_index = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
170 pkt->flags = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
171
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
172 for(i=0; i<FF_INPUT_BUFFER_PADDING_SIZE; i++)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
173 pkt->data[size+i]= 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
174
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
175 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
176 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
177
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
178 /**
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
179 * Free a packet
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
180 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
181 * @param pkt packet to free
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
182 */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
183 void av_free_packet(AVPacket *pkt)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
184 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
185 av_freep(&pkt->data);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
186 /* fail safe */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
187 pkt->size = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
188 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
189
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
190 /* fifo handling */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
191
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
192 int fifo_init(FifoBuffer *f, int size)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
193 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
194 f->buffer = av_malloc(size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
195 if (!f->buffer)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
196 return -1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
197 f->end = f->buffer + size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
198 f->wptr = f->rptr = f->buffer;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
199 return 0;
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 void fifo_free(FifoBuffer *f)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
203 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
204 av_free(f->buffer);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
205 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
206
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
207 int fifo_size(FifoBuffer *f, UINT8 *rptr)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
208 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
209 int size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
210
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
211 if (f->wptr >= rptr) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
212 size = f->wptr - rptr;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
213 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
214 size = (f->end - rptr) + (f->wptr - f->buffer);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
215 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
216 return size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
217 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
218
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
219 /* get data from the fifo (return -1 if not enough data) */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
220 int fifo_read(FifoBuffer *f, UINT8 *buf, int buf_size, UINT8 **rptr_ptr)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
221 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
222 UINT8 *rptr = *rptr_ptr;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
223 int size, len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
224
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
225 if (f->wptr >= rptr) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
226 size = f->wptr - rptr;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
227 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
228 size = (f->end - rptr) + (f->wptr - f->buffer);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
229 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
230
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
231 if (size < buf_size)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
232 return -1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
233 while (buf_size > 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
234 len = f->end - rptr;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
235 if (len > buf_size)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
236 len = buf_size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
237 memcpy(buf, rptr, len);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
238 buf += len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
239 rptr += len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
240 if (rptr >= f->end)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
241 rptr = f->buffer;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
242 buf_size -= len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
243 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
244 *rptr_ptr = rptr;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
245 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
246 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
247
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
248 void fifo_write(FifoBuffer *f, UINT8 *buf, int size, UINT8 **wptr_ptr)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
249 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
250 int len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
251 UINT8 *wptr;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
252 wptr = *wptr_ptr;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
253 while (size > 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
254 len = f->end - wptr;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
255 if (len > size)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
256 len = size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
257 memcpy(wptr, buf, len);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
258 wptr += len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
259 if (wptr >= f->end)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
260 wptr = f->buffer;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
261 buf += len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
262 size -= len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
263 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
264 *wptr_ptr = wptr;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
265 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
266
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
267 int filename_number_test(const char *filename)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
268 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
269 char buf[1024];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
270 return get_frame_filename(buf, sizeof(buf), filename, 1);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
271 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
272
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
273 /* guess file format */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
274 AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
275 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
276 AVInputFormat *fmt1, *fmt;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
277 int score, score_max;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
278
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
279 fmt = NULL;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
280 score_max = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
281 for(fmt1 = first_iformat; fmt1 != NULL; fmt1 = fmt1->next) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
282 if (!is_opened && !(fmt1->flags & AVFMT_NOFILE))
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
283 continue;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
284 score = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
285 if (fmt1->read_probe) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
286 score = fmt1->read_probe(pd);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
287 } else if (fmt1->extensions) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
288 if (match_ext(pd->filename, fmt1->extensions)) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
289 score = 50;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
290 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
291 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
292 if (score > score_max) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
293 score_max = score;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
294 fmt = fmt1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
295 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
296 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
297 return fmt;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
298 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
299
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
300 /************************************************************/
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
301 /* input media file */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
302
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
303 #define PROBE_BUF_SIZE 2048
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
304
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
305 /**
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
306 * Open a media file as input. The codec are not opened. Only the file
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
307 * header (if present) is read.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
308 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
309 * @param ic_ptr the opened media file handle is put here
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
310 * @param filename filename to open.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
311 * @param fmt if non NULL, force the file format to use
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
312 * @param buf_size optional buffer size (zero if default is OK)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
313 * @param ap additionnal parameters needed when opening the file (NULL if default)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
314 * @return 0 if OK. AVERROR_xxx otherwise.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
315 */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
316 int av_open_input_file(AVFormatContext **ic_ptr, const char *filename,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
317 AVInputFormat *fmt,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
318 int buf_size,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
319 AVFormatParameters *ap)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
320 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
321 AVFormatContext *ic = NULL;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
322 int err;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
323 char buf[PROBE_BUF_SIZE];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
324 AVProbeData probe_data, *pd = &probe_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
325
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
326 ic = av_mallocz(sizeof(AVFormatContext));
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
327 if (!ic) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
328 err = AVERROR_NOMEM;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
329 goto fail;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
330 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
331 pstrcpy(ic->filename, sizeof(ic->filename), filename);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
332 pd->filename = ic->filename;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
333 pd->buf = buf;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
334 pd->buf_size = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
335
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
336 if (!fmt) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
337 /* guess format if no file can be opened */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
338 fmt = av_probe_input_format(pd, 0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
339 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
340
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
341 if (!fmt || !(fmt->flags & AVFMT_NOFILE)) {
20
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
342 /* if no file needed do not try to open one */
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
343 if (url_fopen(&ic->pb, filename, URL_RDONLY) < 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
344 err = AVERROR_IO;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
345 goto fail;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
346 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
347 if (buf_size > 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
348 url_setbufsize(&ic->pb, buf_size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
349 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
350 if (!fmt) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
351 /* read probe data */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
352 pd->buf_size = get_buffer(&ic->pb, buf, PROBE_BUF_SIZE);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
353 url_fseek(&ic->pb, 0, SEEK_SET);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
354 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
355 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
356
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
357 /* guess file format */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
358 if (!fmt) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
359 fmt = av_probe_input_format(pd, 1);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
360 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
361
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
362 /* if still no format found, error */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
363 if (!fmt) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
364 err = AVERROR_NOFMT;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
365 goto fail;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
366 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
367
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
368 /* XXX: suppress this hack for redirectors */
22
65433f1b2549 os2 support patch by ("Slavik Gnatenko" <miracle9 at newmail dot ru>)
michaelni
parents: 21
diff changeset
369 #ifdef CONFIG_NETWORK
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
370 if (fmt == &redir_demux) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
371 err = redir_open(ic_ptr, &ic->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
372 url_fclose(&ic->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
373 av_free(ic);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
374 return err;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
375 }
11
932b59c66c60 mingw patch by (Bill Eldridge <bill at rfa dot org>)
michaelni
parents: 9
diff changeset
376 #endif
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
377
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
378 ic->iformat = fmt;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
379
20
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
380 /* check filename in case of an image number is expected */
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
381 if (ic->iformat->flags & AVFMT_NEEDNUMBER) {
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
382 if (filename_number_test(ic->filename) < 0) {
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
383 err = AVERROR_NUMEXPECTED;
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
384 goto fail1;
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
385 }
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
386 }
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
387
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
388 /* allocate private data */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
389 ic->priv_data = av_mallocz(fmt->priv_data_size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
390 if (!ic->priv_data) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
391 err = AVERROR_NOMEM;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
392 goto fail;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
393 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
394
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
395 /* default pts settings is MPEG like */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
396 av_set_pts_info(ic, 33, 1, 90000);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
397
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
398 err = ic->iformat->read_header(ic, ap);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
399 if (err < 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
400 goto fail1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
401 *ic_ptr = ic;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
402 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
403 fail1:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
404 if (!(fmt->flags & AVFMT_NOFILE)) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
405 url_fclose(&ic->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
406 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
407 fail:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
408 if (ic) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
409 av_freep(&ic->priv_data);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
410 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
411 av_free(ic);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
412 *ic_ptr = NULL;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
413 return err;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
414 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
415
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
416 /**
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
417 * Read a packet from a media file
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
418 * @param s media file handle
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
419 * @param pkt is filled
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
420 * @return 0 if OK. AVERROR_xxx if error.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
421 */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
422 int av_read_packet(AVFormatContext *s, AVPacket *pkt)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
423 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
424 AVPacketList *pktl;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
425
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
426 pktl = s->packet_buffer;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
427 if (pktl) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
428 /* read packet from packet buffer, if there is data */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
429 *pkt = pktl->pkt;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
430 s->packet_buffer = pktl->next;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
431 av_free(pktl);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
432 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
433 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
434 return s->iformat->read_packet(s, pkt);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
435 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
436 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
437
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
438 /* state for codec information */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
439 #define CSTATE_NOTFOUND 0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
440 #define CSTATE_DECODING 1
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
441 #define CSTATE_FOUND 2
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
442
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
443 static int has_codec_parameters(AVCodecContext *enc)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
444 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
445 int val;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
446 switch(enc->codec_type) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
447 case CODEC_TYPE_AUDIO:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
448 val = enc->sample_rate;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
449 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
450 case CODEC_TYPE_VIDEO:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
451 val = enc->width;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
452 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
453 default:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
454 val = 1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
455 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
456 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
457 return (val != 0);
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 /**
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
461 * Read the beginning of a media file to get stream information. This
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
462 * is useful for file formats with no headers such as MPEG. This
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
463 * function also compute the real frame rate in case of mpeg2 repeat
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
464 * frame mode.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
465 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
466 * @param ic media file handle
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
467 * @return >=0 if OK. AVERROR_xxx if error.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
468 */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
469 int av_find_stream_info(AVFormatContext *ic)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
470 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
471 int i, count, ret, got_picture, size, read_size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
472 AVCodec *codec;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
473 AVStream *st;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
474 AVPacket *pkt;
7
47e502ac6349 AVVideoFrame -> AVFrame
michaelni
parents: 5
diff changeset
475 AVFrame picture;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
476 AVPacketList *pktl=NULL, **ppktl;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
477 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
478 UINT8 *ptr;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
479 int min_read_size, max_read_size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
480
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
481 /* typical mpeg ts rate is 40 Mbits. DVD rate is about 10
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
482 Mbits. We read at most 0.1 second of file to find all streams */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
483
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
484 /* XXX: base it on stream bitrate when possible */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
485 if (ic->iformat == &mpegts_demux) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
486 /* maximum number of bytes we accept to read to find all the streams
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
487 in a file */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
488 min_read_size = 3000000;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
489 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
490 min_read_size = 125000;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
491 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
492 /* max read size is 2 seconds of video max */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
493 max_read_size = min_read_size * 20;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
494
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
495 /* set initial codec state */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
496 for(i=0;i<ic->nb_streams;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
497 st = ic->streams[i];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
498 if (has_codec_parameters(&st->codec))
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
499 st->codec_info_state = CSTATE_FOUND;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
500 else
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
501 st->codec_info_state = CSTATE_NOTFOUND;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
502 st->codec_info_nb_repeat_frames = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
503 st->codec_info_nb_real_frames = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
504 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
505
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
506 count = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
507 read_size = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
508 ppktl = &ic->packet_buffer;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
509 for(;;) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
510 /* check if one codec still needs to be handled */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
511 for(i=0;i<ic->nb_streams;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
512 st = ic->streams[i];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
513 if (st->codec_info_state != CSTATE_FOUND)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
514 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
515 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
516 if (i == ic->nb_streams) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
517 /* NOTE: if the format has no header, then we need to read
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
518 some packets to get most of the streams, so we cannot
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
519 stop here */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
520 if (!(ic->iformat->flags & AVFMT_NOHEADER) ||
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
521 read_size >= min_read_size) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
522 /* if we found the info for all the codecs, we can stop */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
523 ret = count;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
524 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
525 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
526 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
527 /* we did not get all the codec info, but we read too much data */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
528 if (read_size >= max_read_size) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
529 ret = count;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
530 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
531 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
532 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
533
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
534 pktl = av_mallocz(sizeof(AVPacketList));
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
535 if (!pktl) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
536 ret = AVERROR_NOMEM;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
537 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
538 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
539
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
540 /* add the packet in the buffered packet list */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
541 *ppktl = pktl;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
542 ppktl = &pktl->next;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
543
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
544 /* NOTE: a new stream can be added there if no header in file
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
545 (AVFMT_NOHEADER) */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
546 pkt = &pktl->pkt;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
547 if (ic->iformat->read_packet(ic, pkt) < 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
548 /* EOF or error */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
549 ret = -1; /* we could not have all the codec parameters before EOF */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
550 if ((ic->iformat->flags & AVFMT_NOHEADER) &&
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
551 i == ic->nb_streams)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
552 ret = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
553 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
554 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
555 read_size += pkt->size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
556
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
557 /* open new codecs */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
558 for(i=0;i<ic->nb_streams;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
559 st = ic->streams[i];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
560 if (st->codec_info_state == CSTATE_NOTFOUND) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
561 /* set to found in case of error */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
562 st->codec_info_state = CSTATE_FOUND;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
563 codec = avcodec_find_decoder(st->codec.codec_id);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
564 if (codec) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
565 if(codec->capabilities & CODEC_CAP_TRUNCATED)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
566 st->codec.flags |= CODEC_FLAG_TRUNCATED;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
567
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
568 ret = avcodec_open(&st->codec, codec);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
569 if (ret >= 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
570 st->codec_info_state = CSTATE_DECODING;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
571 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
572 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
573 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
574
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
575 st = ic->streams[pkt->stream_index];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
576 if (st->codec_info_state == CSTATE_DECODING) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
577 /* decode the data and update codec parameters */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
578 ptr = pkt->data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
579 size = pkt->size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
580 while (size > 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
581 switch(st->codec.codec_type) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
582 case CODEC_TYPE_VIDEO:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
583 ret = avcodec_decode_video(&st->codec, &picture,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
584 &got_picture, ptr, size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
585 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
586 case CODEC_TYPE_AUDIO:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
587 ret = avcodec_decode_audio(&st->codec, samples,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
588 &got_picture, ptr, size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
589 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
590 default:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
591 ret = -1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
592 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
593 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
594 if (ret < 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
595 /* if error, simply ignore because another packet
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
596 may be OK */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
597 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
598 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
599 if (got_picture) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
600 /* we got the parameters - now we can stop
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
601 examining this stream */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
602 /* XXX: add a codec info so that we can decide if
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
603 the codec can repeat frames */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
604 if (st->codec.codec_id == CODEC_ID_MPEG1VIDEO &&
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
605 ic->iformat != &mpegts_demux &&
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
606 st->codec.sub_id == 2) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
607 /* for mpeg2 video, we want to know the real
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
608 frame rate, so we decode 40 frames. In mpeg
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
609 TS case we do not do it because it would be
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
610 too long */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
611 st->codec_info_nb_real_frames++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
612 st->codec_info_nb_repeat_frames += st->codec.repeat_pict;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
613 #if 0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
614 /* XXX: testing */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
615 if ((st->codec_info_nb_real_frames % 24) == 23) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
616 st->codec_info_nb_repeat_frames += 2;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
617 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
618 #endif
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
619 /* stop after 40 frames */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
620 if (st->codec_info_nb_real_frames >= 40) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
621 st->r_frame_rate = (st->codec.frame_rate *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
622 st->codec_info_nb_real_frames) /
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
623 (st->codec_info_nb_real_frames +
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
624 (st->codec_info_nb_repeat_frames >> 1));
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
625 goto close_codec;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
626 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
627 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
628 close_codec:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
629 st->codec_info_state = CSTATE_FOUND;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
630 avcodec_close(&st->codec);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
631 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
632 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
633 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
634 ptr += ret;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
635 size -= ret;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
636 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
637 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
638 count++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
639 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
640
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
641 /* close each codec if there are opened */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
642 for(i=0;i<ic->nb_streams;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
643 st = ic->streams[i];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
644 if (st->codec_info_state == CSTATE_DECODING)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
645 avcodec_close(&st->codec);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
646 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
647
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
648 /* set real frame rate info */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
649 for(i=0;i<ic->nb_streams;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
650 st = ic->streams[i];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
651 if (st->codec.codec_type == CODEC_TYPE_VIDEO) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
652 if (!st->r_frame_rate)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
653 st->r_frame_rate = st->codec.frame_rate;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
654 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
655 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
656
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
657 return ret;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
658 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
659
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
660 /**
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
661 * Close a media file (but not its codecs)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
662 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
663 * @param s media file handle
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
664 */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
665 void av_close_input_file(AVFormatContext *s)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
666 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
667 int i;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
668
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
669 if (s->iformat->read_close)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
670 s->iformat->read_close(s);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
671 for(i=0;i<s->nb_streams;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
672 av_free(s->streams[i]);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
673 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
674 if (s->packet_buffer) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
675 AVPacketList *p, *p1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
676 p = s->packet_buffer;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
677 while (p != NULL) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
678 p1 = p->next;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
679 av_free_packet(&p->pkt);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
680 av_free(p);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
681 p = p1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
682 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
683 s->packet_buffer = NULL;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
684 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
685 if (!(s->iformat->flags & AVFMT_NOFILE)) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
686 url_fclose(&s->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
687 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
688 av_freep(&s->priv_data);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
689 av_free(s);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
690 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
691
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
692 /**
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
693 * Add a new stream to a media file. Can only be called in the
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
694 * read_header function. If the flag AVFMT_NOHEADER is in the format
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
695 * description, then new streams can be added in read_packet too.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
696 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
697 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
698 * @param s media file handle
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
699 * @param id file format dependent stream id
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
700 */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
701 AVStream *av_new_stream(AVFormatContext *s, int id)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
702 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
703 AVStream *st;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
704
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
705 if (s->nb_streams >= MAX_STREAMS)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
706 return NULL;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
707
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
708 st = av_mallocz(sizeof(AVStream));
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
709 if (!st)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
710 return NULL;
5
39c4c4336486 cleanup
michaelni
parents: 0
diff changeset
711 avcodec_get_context_defaults(&st->codec);
39c4c4336486 cleanup
michaelni
parents: 0
diff changeset
712
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
713 st->index = s->nb_streams;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
714 st->id = id;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
715 s->streams[s->nb_streams++] = st;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
716 return st;
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 /************************************************************/
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
720 /* output media file */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
721
20
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
722 int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap)
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
723 {
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
724 int ret;
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
725
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
726 s->priv_data = av_mallocz(s->oformat->priv_data_size);
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
727 if (!s->priv_data)
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
728 return AVERROR_NOMEM;
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
729 if (s->oformat->set_parameters) {
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
730 ret = s->oformat->set_parameters(s, ap);
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
731 if (ret < 0)
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
732 return ret;
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
733 }
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
734 return 0;
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
735 }
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
736
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
737 /**
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
738 * allocate the stream private data and write the stream header to an
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
739 * output media file
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
740 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
741 * @param s media file handle
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
742 * @return 0 if OK. AVERROR_xxx if error.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
743 */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
744 int av_write_header(AVFormatContext *s)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
745 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
746 int ret, i;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
747 AVStream *st;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
748
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
749 /* default pts settings is MPEG like */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
750 av_set_pts_info(s, 33, 1, 90000);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
751 ret = s->oformat->write_header(s);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
752 if (ret < 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
753 return ret;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
754
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
755 /* init PTS generation */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
756 for(i=0;i<s->nb_streams;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
757 st = s->streams[i];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
758
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
759 switch (st->codec.codec_type) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
760 case CODEC_TYPE_AUDIO:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
761 av_frac_init(&st->pts, 0, 0,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
762 (INT64)s->pts_num * st->codec.sample_rate);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
763 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
764 case CODEC_TYPE_VIDEO:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
765 av_frac_init(&st->pts, 0, 0,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
766 (INT64)s->pts_num * st->codec.frame_rate);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
767 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
768 default:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
769 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
770 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
771 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
772 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
773 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
774
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
775 /**
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
776 * Write a packet to an output media file. The packet shall contain
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
777 * one audio or video frame.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
778 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
779 * @param s media file handle
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
780 * @param stream_index stream index
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
781 * @param buf buffer containing the frame data
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
782 * @param size size of buffer
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
783 * @return < 0 if error, = 0 if OK, 1 if end of stream wanted.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
784 */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
785 int av_write_frame(AVFormatContext *s, int stream_index, const uint8_t *buf,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
786 int size)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
787 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
788 AVStream *st;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
789 INT64 pts_mask;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
790 int ret, frame_size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
791
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
792 st = s->streams[stream_index];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
793 pts_mask = (1LL << s->pts_wrap_bits) - 1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
794 ret = s->oformat->write_packet(s, stream_index, (uint8_t *)buf, size,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
795 st->pts.val & pts_mask);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
796 if (ret < 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
797 return ret;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
798
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
799 /* update pts */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
800 switch (st->codec.codec_type) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
801 case CODEC_TYPE_AUDIO:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
802 if (st->codec.frame_size <= 1) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
803 frame_size = size / st->codec.channels;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
804 /* specific hack for pcm codecs because no frame size is provided */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
805 switch(st->codec.codec_id) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
806 case CODEC_ID_PCM_S16LE:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
807 case CODEC_ID_PCM_S16BE:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
808 case CODEC_ID_PCM_U16LE:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
809 case CODEC_ID_PCM_U16BE:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
810 frame_size >>= 1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
811 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
812 default:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
813 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
814 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
815 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
816 frame_size = st->codec.frame_size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
817 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
818 av_frac_add(&st->pts,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
819 (INT64)s->pts_den * frame_size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
820 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
821 case CODEC_TYPE_VIDEO:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
822 av_frac_add(&st->pts,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
823 (INT64)s->pts_den * FRAME_RATE_BASE);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
824 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
825 default:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
826 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
827 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
828 return ret;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
829 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
830
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
831 /**
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
832 * write the stream trailer to an output media file and and free the
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
833 * file private data.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
834 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
835 * @param s media file handle
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
836 * @return 0 if OK. AVERROR_xxx if error. */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
837 int av_write_trailer(AVFormatContext *s)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
838 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
839 int ret;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
840 ret = s->oformat->write_trailer(s);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
841 av_freep(&s->priv_data);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
842 return ret;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
843 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
844
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
845 /* "user interface" functions */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
846
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
847 void dump_format(AVFormatContext *ic,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
848 int index,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
849 const char *url,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
850 int is_output)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
851 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
852 int i, flags;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
853 char buf[256];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
854
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
855 fprintf(stderr, "%s #%d, %s, %s '%s':\n",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
856 is_output ? "Output" : "Input",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
857 index,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
858 is_output ? ic->oformat->name : ic->iformat->name,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
859 is_output ? "to" : "from", url);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
860 for(i=0;i<ic->nb_streams;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
861 AVStream *st = ic->streams[i];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
862 avcodec_string(buf, sizeof(buf), &st->codec, is_output);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
863 fprintf(stderr, " Stream #%d.%d", index, i);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
864 /* the pid is an important information, so we display it */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
865 /* XXX: add a generic system */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
866 if (is_output)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
867 flags = ic->oformat->flags;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
868 else
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
869 flags = ic->iformat->flags;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
870 if (flags & AVFMT_SHOW_IDS) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
871 fprintf(stderr, "[0x%x]", st->id);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
872 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
873 fprintf(stderr, ": %s\n", buf);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
874 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
875 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
876
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
877 typedef struct {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
878 const char *str;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
879 int width, height;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
880 } SizeEntry;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
881
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
882 static SizeEntry sizes[] = {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
883 { "sqcif", 128, 96 },
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
884 { "qcif", 176, 144 },
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
885 { "cif", 352, 288 },
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
886 { "4cif", 704, 576 },
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
887 };
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
888
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
889 int parse_image_size(int *width_ptr, int *height_ptr, const char *str)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
890 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
891 int i;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
892 int n = sizeof(sizes) / sizeof(SizeEntry);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
893 const char *p;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
894 int frame_width = 0, frame_height = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
895
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
896 for(i=0;i<n;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
897 if (!strcmp(sizes[i].str, str)) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
898 frame_width = sizes[i].width;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
899 frame_height = sizes[i].height;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
900 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
901 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
902 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
903 if (i == n) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
904 p = str;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
905 frame_width = strtol(p, (char **)&p, 10);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
906 if (*p)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
907 p++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
908 frame_height = strtol(p, (char **)&p, 10);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
909 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
910 if (frame_width <= 0 || frame_height <= 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
911 return -1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
912 *width_ptr = frame_width;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
913 *height_ptr = frame_height;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
914 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
915 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
916
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
917 INT64 av_gettime(void)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
918 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
919 #ifdef CONFIG_WIN32
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
920 struct _timeb tb;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
921 _ftime(&tb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
922 return ((INT64)tb.time * INT64_C(1000) + (INT64)tb.millitm) * INT64_C(1000);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
923 #else
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
924 struct timeval tv;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
925 gettimeofday(&tv,NULL);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
926 return (INT64)tv.tv_sec * 1000000 + tv.tv_usec;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
927 #endif
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
928 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
929
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
930 static time_t mktimegm(struct tm *tm)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
931 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
932 time_t t;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
933
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
934 int y = tm->tm_year + 1900, m = tm->tm_mon + 1, d = tm->tm_mday;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
935
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
936 if (m < 3) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
937 m += 12;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
938 y--;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
939 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
940
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
941 t = 86400 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
942 (d + (153 * m - 457) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 719469);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
943
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
944 t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
945
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
946 return t;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
947 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
948
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
949 /* Syntax:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
950 * - If not a duration:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
951 * [{YYYY-MM-DD|YYYYMMDD}]{T| }{HH[:MM[:SS[.m...]]][Z]|HH[MM[SS[.m...]]][Z]}
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
952 * Time is localtime unless Z is suffixed to the end. In this case GMT
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
953 * Return the date in micro seconds since 1970
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
954 * - If duration:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
955 * HH[:MM[:SS[.m...]]]
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
956 * S+[.m...]
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
957 */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
958 INT64 parse_date(const char *datestr, int duration)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
959 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
960 const char *p;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
961 INT64 t;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
962 struct tm dt;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
963 int i;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
964 static const char *date_fmt[] = {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
965 "%Y-%m-%d",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
966 "%Y%m%d",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
967 };
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
968 static const char *time_fmt[] = {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
969 "%H:%M:%S",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
970 "%H%M%S",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
971 };
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
972 const char *q;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
973 int is_utc, len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
974 char lastch;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
975 time_t now = time(0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
976
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
977 len = strlen(datestr);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
978 if (len > 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
979 lastch = datestr[len - 1];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
980 else
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
981 lastch = '\0';
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
982 is_utc = (lastch == 'z' || lastch == 'Z');
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
983
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
984 memset(&dt, 0, sizeof(dt));
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
985
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
986 p = datestr;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
987 q = NULL;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
988 if (!duration) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
989 for (i = 0; i < sizeof(date_fmt) / sizeof(date_fmt[0]); i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
990 q = strptime(p, date_fmt[i], &dt);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
991 if (q) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
992 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
993 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
994 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
995
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
996 if (!q) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
997 if (is_utc) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
998 dt = *gmtime(&now);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
999 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1000 dt = *localtime(&now);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1001 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1002 dt.tm_hour = dt.tm_min = dt.tm_sec = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1003 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1004 p = q;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1005 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1006
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1007 if (*p == 'T' || *p == 't' || *p == ' ')
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1008 p++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1009
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1010 for (i = 0; i < sizeof(time_fmt) / sizeof(time_fmt[0]); i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1011 q = strptime(p, time_fmt[i], &dt);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1012 if (q) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1013 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1014 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1015 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1016 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1017 q = strptime(p, time_fmt[0], &dt);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1018 if (!q) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1019 dt.tm_sec = strtol(p, (char **)&q, 10);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1020 dt.tm_min = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1021 dt.tm_hour = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1022 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1023 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1024
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1025 /* Now we have all the fields that we can get */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1026 if (!q) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1027 if (duration)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1028 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1029 else
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1030 return now * INT64_C(1000000);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1031 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1032
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1033 if (duration) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1034 t = dt.tm_hour * 3600 + dt.tm_min * 60 + dt.tm_sec;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1035 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1036 dt.tm_isdst = -1; /* unknown */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1037 if (is_utc) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1038 t = mktimegm(&dt);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1039 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1040 t = mktime(&dt);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1041 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1042 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1043
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1044 t *= 1000000;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1045
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1046 if (*q == '.') {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1047 int val, n;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1048 q++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1049 for (val = 0, n = 100000; n >= 1; n /= 10, q++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1050 if (!isdigit(*q))
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1051 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1052 val += n * (*q - '0');
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1053 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1054 t += val;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1055 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1056 return t;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1057 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1058
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1059 /* syntax: '?tag1=val1&tag2=val2...'. Little URL decoding is done. Return
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1060 1 if found */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1061 int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1062 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1063 const char *p;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1064 char tag[128], *q;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1065
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1066 p = info;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1067 if (*p == '?')
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1068 p++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1069 for(;;) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1070 q = tag;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1071 while (*p != '\0' && *p != '=' && *p != '&') {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1072 if ((q - tag) < sizeof(tag) - 1)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1073 *q++ = *p;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1074 p++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1075 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1076 *q = '\0';
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1077 q = arg;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1078 if (*p == '=') {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1079 p++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1080 while (*p != '&' && *p != '\0') {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1081 if ((q - arg) < arg_size - 1) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1082 if (*p == '+')
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1083 *q++ = ' ';
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1084 else
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1085 *q++ = *p;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1086 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1087 p++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1088 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1089 *q = '\0';
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1090 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1091 if (!strcmp(tag, tag1))
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1092 return 1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1093 if (*p != '&')
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1094 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1095 p++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1096 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1097 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1098 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1099
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1100 /* Return in 'buf' the path with '%d' replaced by number. Also handles
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1101 the '%0nd' format where 'n' is the total number of digits and
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1102 '%%'. Return 0 if OK, and -1 if format error */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1103 int get_frame_filename(char *buf, int buf_size,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1104 const char *path, int number)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1105 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1106 const char *p;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1107 char *q, buf1[20];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1108 int nd, len, c, percentd_found;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1109
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1110 q = buf;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1111 p = path;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1112 percentd_found = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1113 for(;;) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1114 c = *p++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1115 if (c == '\0')
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1116 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1117 if (c == '%') {
9
97e61383cb81 * Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents: 7
diff changeset
1118 do {
97e61383cb81 * Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents: 7
diff changeset
1119 nd = 0;
97e61383cb81 * Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents: 7
diff changeset
1120 while (isdigit(*p)) {
97e61383cb81 * Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents: 7
diff changeset
1121 nd = nd * 10 + *p++ - '0';
97e61383cb81 * Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents: 7
diff changeset
1122 }
97e61383cb81 * Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents: 7
diff changeset
1123 c = *p++;
97e61383cb81 * Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents: 7
diff changeset
1124 if (c == '*' && nd > 0) {
97e61383cb81 * Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents: 7
diff changeset
1125 // The nd field is actually the modulus
97e61383cb81 * Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents: 7
diff changeset
1126 number = number % nd;
97e61383cb81 * Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents: 7
diff changeset
1127 c = *p++;
97e61383cb81 * Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents: 7
diff changeset
1128 nd = 0;
97e61383cb81 * Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents: 7
diff changeset
1129 }
97e61383cb81 * Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents: 7
diff changeset
1130 } while (isdigit(c));
97e61383cb81 * Extend the syntax of a filename for the img reader to allow looping. Thus
philipjsg
parents: 7
diff changeset
1131
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1132 switch(c) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1133 case '%':
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1134 goto addchar;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1135 case 'd':
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1136 if (percentd_found)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1137 goto fail;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1138 percentd_found = 1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1139 snprintf(buf1, sizeof(buf1), "%0*d", nd, number);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1140 len = strlen(buf1);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1141 if ((q - buf + len) > buf_size - 1)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1142 goto fail;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1143 memcpy(q, buf1, len);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1144 q += len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1145 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1146 default:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1147 goto fail;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1148 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1149 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1150 addchar:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1151 if ((q - buf) < buf_size - 1)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1152 *q++ = c;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1153 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1154 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1155 if (!percentd_found)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1156 goto fail;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1157 *q = '\0';
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1158 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1159 fail:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1160 *q = '\0';
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1161 return -1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1162 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1163
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1164 /**
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1165 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1166 * Print on stdout a nice hexa dump of a buffer
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1167 * @param buf buffer
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1168 * @param size buffer size
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1169 */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1170 void av_hex_dump(UINT8 *buf, int size)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1171 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1172 int len, i, j, c;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1173
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1174 for(i=0;i<size;i+=16) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1175 len = size - i;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1176 if (len > 16)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1177 len = 16;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1178 printf("%08x ", i);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1179 for(j=0;j<16;j++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1180 if (j < len)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1181 printf(" %02x", buf[i+j]);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1182 else
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1183 printf(" ");
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1184 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1185 printf(" ");
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1186 for(j=0;j<len;j++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1187 c = buf[i+j];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1188 if (c < ' ' || c > '~')
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1189 c = '.';
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1190 printf("%c", c);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1191 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1192 printf("\n");
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1193 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1194 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1195
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1196 void url_split(char *proto, int proto_size,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1197 char *hostname, int hostname_size,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1198 int *port_ptr,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1199 char *path, int path_size,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1200 const char *url)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1201 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1202 const char *p;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1203 char *q;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1204 int port;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1205
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1206 port = -1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1207
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1208 p = url;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1209 q = proto;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1210 while (*p != ':' && *p != '\0') {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1211 if ((q - proto) < proto_size - 1)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1212 *q++ = *p;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1213 p++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1214 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1215 if (proto_size > 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1216 *q = '\0';
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1217 if (*p == '\0') {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1218 if (proto_size > 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1219 proto[0] = '\0';
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1220 if (hostname_size > 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1221 hostname[0] = '\0';
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1222 p = url;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1223 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1224 p++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1225 if (*p == '/')
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1226 p++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1227 if (*p == '/')
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1228 p++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1229 q = hostname;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1230 while (*p != ':' && *p != '/' && *p != '?' && *p != '\0') {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1231 if ((q - hostname) < hostname_size - 1)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1232 *q++ = *p;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1233 p++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1234 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1235 if (hostname_size > 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1236 *q = '\0';
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1237 if (*p == ':') {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1238 p++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1239 port = strtoul(p, (char **)&p, 10);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1240 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1241 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1242 if (port_ptr)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1243 *port_ptr = port;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1244 pstrcpy(path, path_size, p);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1245 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1246
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1247 /**
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1248 * Set the pts for a given stream
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1249 * @param s stream
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1250 * @param pts_wrap_bits number of bits effectively used by the pts
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1251 * (used for wrap control, 33 is the value for MPEG)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1252 * @param pts_num numerator to convert to seconds (MPEG: 1)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1253 * @param pts_den denominator to convert to seconds (MPEG: 90000)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1254 */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1255 void av_set_pts_info(AVFormatContext *s, int pts_wrap_bits,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1256 int pts_num, int pts_den)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1257 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1258 s->pts_wrap_bits = pts_wrap_bits;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1259 s->pts_num = pts_num;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1260 s->pts_den = pts_den;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1261 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1262
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1263 /* fraction handling */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1264
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1265 /**
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1266 * f = val + (num / den) + 0.5. 'num' is normalized so that it is such
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1267 * as 0 <= num < den.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1268 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1269 * @param f fractional number
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1270 * @param val integer value
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1271 * @param num must be >= 0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1272 * @param den must be >= 1
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1273 */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1274 void av_frac_init(AVFrac *f, INT64 val, INT64 num, INT64 den)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1275 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1276 num += (den >> 1);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1277 if (num >= den) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1278 val += num / den;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1279 num = num % den;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1280 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1281 f->val = val;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1282 f->num = num;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1283 f->den = den;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1284 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1285
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1286 /* set f to (val + 0.5) */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1287 void av_frac_set(AVFrac *f, INT64 val)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1288 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1289 f->val = val;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1290 f->num = f->den >> 1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1291 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1292
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1293 /**
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1294 * Fractionnal addition to f: f = f + (incr / f->den)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1295 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1296 * @param f fractional number
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1297 * @param incr increment, can be positive or negative
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1298 */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1299 void av_frac_add(AVFrac *f, INT64 incr)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1300 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1301 INT64 num, den;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1302
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1303 num = f->num + incr;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1304 den = f->den;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1305 if (num < 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1306 f->val += num / den;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1307 num = num % den;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1308 if (num < 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1309 num += den;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1310 f->val--;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1311 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1312 } else if (num >= den) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1313 f->val += num / den;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1314 num = num % den;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1315 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1316 f->num = num;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1317 }
20
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1318
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1319 /**
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1320 * register a new image format
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1321 * @param img_fmt Image format descriptor
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1322 */
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1323 void av_register_image_format(AVImageFormat *img_fmt)
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1324 {
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1325 AVImageFormat **p;
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1326
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1327 p = &first_image_format;
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1328 while (*p != NULL) p = &(*p)->next;
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1329 *p = img_fmt;
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1330 img_fmt->next = NULL;
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1331 }
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1332
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1333 /* guess image format */
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1334 AVImageFormat *av_probe_image_format(AVProbeData *pd)
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1335 {
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1336 AVImageFormat *fmt1, *fmt;
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1337 int score, score_max;
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1338
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1339 fmt = NULL;
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1340 score_max = 0;
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1341 for(fmt1 = first_image_format; fmt1 != NULL; fmt1 = fmt1->next) {
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1342 if (fmt1->img_probe) {
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1343 score = fmt1->img_probe(pd);
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1344 if (score > score_max) {
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1345 score_max = score;
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1346 fmt = fmt1;
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1347 }
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1348 }
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1349 }
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1350 return fmt;
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1351 }
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1352
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1353 AVImageFormat *guess_image_format(const char *filename)
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1354 {
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1355 AVImageFormat *fmt1;
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1356
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1357 for(fmt1 = first_image_format; fmt1 != NULL; fmt1 = fmt1->next) {
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1358 if (fmt1->extensions && match_ext(filename, fmt1->extensions))
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1359 return fmt1;
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1360 }
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1361 return NULL;
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1362 }
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1363
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1364 /**
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1365 * Read an image from a stream.
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1366 * @param gb byte stream containing the image
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1367 * @param fmt image format, NULL if probing is required
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1368 */
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1369 int av_read_image(ByteIOContext *pb, const char *filename,
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1370 AVImageFormat *fmt,
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1371 int (*alloc_cb)(void *, AVImageInfo *info), void *opaque)
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1372 {
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1373 char buf[PROBE_BUF_SIZE];
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1374 AVProbeData probe_data, *pd = &probe_data;
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1375 offset_t pos;
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1376 int ret;
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1377
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1378 if (!fmt) {
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1379 pd->filename = (char *)filename;
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1380 pd->buf = buf;
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1381 pos = url_ftell(pb);
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1382 pd->buf_size = get_buffer(pb, buf, PROBE_BUF_SIZE);
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1383 url_fseek(pb, pos, SEEK_SET);
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1384 fmt = av_probe_image_format(pd);
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1385 }
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1386 if (!fmt)
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1387 return AVERROR_NOFMT;
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1388 ret = fmt->img_read(pb, alloc_cb, opaque);
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1389 return ret;
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1390 }
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1391
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1392 /**
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1393 * Write an image to a stream.
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1394 * @param pb byte stream for the image output
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1395 * @param fmt image format
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1396 * @param img image data and informations
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1397 */
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1398 int av_write_image(ByteIOContext *pb, AVImageFormat *fmt, AVImageInfo *img)
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1399 {
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1400 return fmt->img_write(pb, img);
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1401 }
3d52de18ecc3 added still image support
bellard
parents: 11
diff changeset
1402