Revision 1b58d58d

View differences:

COPYING
1
		    GNU GENERAL PUBLIC LICENSE
2
		       Version 2, June 1991
3

  
4
 Copyright (C) 1989, 1991 Free Software Foundation, Inc.
5
                          675 Mass Ave, Cambridge, MA 02139, USA
6
 Everyone is permitted to copy and distribute verbatim copies
7
 of this license document, but changing it is not allowed.
8

  
9
			    Preamble
10

  
11
  The licenses for most software are designed to take away your
12
freedom to share and change it.  By contrast, the GNU General Public
13
License is intended to guarantee your freedom to share and change free
14
software--to make sure the software is free for all its users.  This
15
General Public License applies to most of the Free Software
16
Foundation's software and to any other program whose authors commit to
17
using it.  (Some other Free Software Foundation software is covered by
18
the GNU Library General Public License instead.)  You can apply it to
19
your programs, too.
20

  
21
  When we speak of free software, we are referring to freedom, not
22
price.  Our General Public Licenses are designed to make sure that you
23
have the freedom to distribute copies of free software (and charge for
24
this service if you wish), that you receive source code or can get it
25
if you want it, that you can change the software or use pieces of it
26
in new free programs; and that you know you can do these things.
27

  
28
  To protect your rights, we need to make restrictions that forbid
29
anyone to deny you these rights or to ask you to surrender the rights.
30
These restrictions translate to certain responsibilities for you if you
31
distribute copies of the software, or if you modify it.
32

  
33
  For example, if you distribute copies of such a program, whether
34
gratis or for a fee, you must give the recipients all the rights that
35
you have.  You must make sure that they, too, receive or can get the
36
source code.  And you must show them these terms so they know their
37
rights.
38

  
39
  We protect your rights with two steps: (1) copyright the software, and
40
(2) offer you this license which gives you legal permission to copy,
41
distribute and/or modify the software.
42

  
43
  Also, for each author's protection and ours, we want to make certain
44
that everyone understands that there is no warranty for this free
45
software.  If the software is modified by someone else and passed on, we
46
want its recipients to know that what they have is not the original, so
47
that any problems introduced by others will not reflect on the original
48
authors' reputations.
49

  
50
  Finally, any free program is threatened constantly by software
51
patents.  We wish to avoid the danger that redistributors of a free
52
program will individually obtain patent licenses, in effect making the
53
program proprietary.  To prevent this, we have made it clear that any
54
patent must be licensed for everyone's free use or not licensed at all.
55

  
56
  The precise terms and conditions for copying, distribution and
57
modification follow.
58

59
		    GNU GENERAL PUBLIC LICENSE
60
   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61

  
62
  0. This License applies to any program or other work which contains
63
a notice placed by the copyright holder saying it may be distributed
64
under the terms of this General Public License.  The "Program", below,
65
refers to any such program or work, and a "work based on the Program"
66
means either the Program or any derivative work under copyright law:
67
that is to say, a work containing the Program or a portion of it,
68
either verbatim or with modifications and/or translated into another
69
language.  (Hereinafter, translation is included without limitation in
70
the term "modification".)  Each licensee is addressed as "you".
71

  
72
Activities other than copying, distribution and modification are not
73
covered by this License; they are outside its scope.  The act of
74
running the Program is not restricted, and the output from the Program
75
is covered only if its contents constitute a work based on the
76
Program (independent of having been made by running the Program).
77
Whether that is true depends on what the Program does.
78

  
79
  1. You may copy and distribute verbatim copies of the Program's
80
source code as you receive it, in any medium, provided that you
81
conspicuously and appropriately publish on each copy an appropriate
82
copyright notice and disclaimer of warranty; keep intact all the
83
notices that refer to this License and to the absence of any warranty;
84
and give any other recipients of the Program a copy of this License
85
along with the Program.
86

  
87
You may charge a fee for the physical act of transferring a copy, and
88
you may at your option offer warranty protection in exchange for a fee.
89

  
90
  2. You may modify your copy or copies of the Program or any portion
91
of it, thus forming a work based on the Program, and copy and
92
distribute such modifications or work under the terms of Section 1
93
above, provided that you also meet all of these conditions:
94

  
95
    a) You must cause the modified files to carry prominent notices
96
    stating that you changed the files and the date of any change.
97

  
98
    b) You must cause any work that you distribute or publish, that in
99
    whole or in part contains or is derived from the Program or any
100
    part thereof, to be licensed as a whole at no charge to all third
101
    parties under the terms of this License.
102

  
103
    c) If the modified program normally reads commands interactively
104
    when run, you must cause it, when started running for such
105
    interactive use in the most ordinary way, to print or display an
106
    announcement including an appropriate copyright notice and a
107
    notice that there is no warranty (or else, saying that you provide
108
    a warranty) and that users may redistribute the program under
109
    these conditions, and telling the user how to view a copy of this
110
    License.  (Exception: if the Program itself is interactive but
111
    does not normally print such an announcement, your work based on
112
    the Program is not required to print an announcement.)
113

114
These requirements apply to the modified work as a whole.  If
115
identifiable sections of that work are not derived from the Program,
116
and can be reasonably considered independent and separate works in
117
themselves, then this License, and its terms, do not apply to those
118
sections when you distribute them as separate works.  But when you
119
distribute the same sections as part of a whole which is a work based
120
on the Program, the distribution of the whole must be on the terms of
121
this License, whose permissions for other licensees extend to the
122
entire whole, and thus to each and every part regardless of who wrote it.
123

  
124
Thus, it is not the intent of this section to claim rights or contest
125
your rights to work written entirely by you; rather, the intent is to
126
exercise the right to control the distribution of derivative or
127
collective works based on the Program.
128

  
129
In addition, mere aggregation of another work not based on the Program
130
with the Program (or with a work based on the Program) on a volume of
131
a storage or distribution medium does not bring the other work under
132
the scope of this License.
133

  
134
  3. You may copy and distribute the Program (or a work based on it,
135
under Section 2) in object code or executable form under the terms of
136
Sections 1 and 2 above provided that you also do one of the following:
137

  
138
    a) Accompany it with the complete corresponding machine-readable
139
    source code, which must be distributed under the terms of Sections
140
    1 and 2 above on a medium customarily used for software interchange; or,
141

  
142
    b) Accompany it with a written offer, valid for at least three
143
    years, to give any third party, for a charge no more than your
144
    cost of physically performing source distribution, a complete
145
    machine-readable copy of the corresponding source code, to be
146
    distributed under the terms of Sections 1 and 2 above on a medium
147
    customarily used for software interchange; or,
148

  
149
    c) Accompany it with the information you received as to the offer
150
    to distribute corresponding source code.  (This alternative is
151
    allowed only for noncommercial distribution and only if you
152
    received the program in object code or executable form with such
153
    an offer, in accord with Subsection b above.)
154

  
155
The source code for a work means the preferred form of the work for
156
making modifications to it.  For an executable work, complete source
157
code means all the source code for all modules it contains, plus any
158
associated interface definition files, plus the scripts used to
159
control compilation and installation of the executable.  However, as a
160
special exception, the source code distributed need not include
161
anything that is normally distributed (in either source or binary
162
form) with the major components (compiler, kernel, and so on) of the
163
operating system on which the executable runs, unless that component
164
itself accompanies the executable.
165

  
166
If distribution of executable or object code is made by offering
167
access to copy from a designated place, then offering equivalent
168
access to copy the source code from the same place counts as
169
distribution of the source code, even though third parties are not
170
compelled to copy the source along with the object code.
171

172
  4. You may not copy, modify, sublicense, or distribute the Program
173
except as expressly provided under this License.  Any attempt
174
otherwise to copy, modify, sublicense or distribute the Program is
175
void, and will automatically terminate your rights under this License.
176
However, parties who have received copies, or rights, from you under
177
this License will not have their licenses terminated so long as such
178
parties remain in full compliance.
179

  
180
  5. You are not required to accept this License, since you have not
181
signed it.  However, nothing else grants you permission to modify or
182
distribute the Program or its derivative works.  These actions are
183
prohibited by law if you do not accept this License.  Therefore, by
184
modifying or distributing the Program (or any work based on the
185
Program), you indicate your acceptance of this License to do so, and
186
all its terms and conditions for copying, distributing or modifying
187
the Program or works based on it.
188

  
189
  6. Each time you redistribute the Program (or any work based on the
190
Program), the recipient automatically receives a license from the
191
original licensor to copy, distribute or modify the Program subject to
192
these terms and conditions.  You may not impose any further
193
restrictions on the recipients' exercise of the rights granted herein.
194
You are not responsible for enforcing compliance by third parties to
195
this License.
196

  
197
  7. If, as a consequence of a court judgment or allegation of patent
198
infringement or for any other reason (not limited to patent issues),
199
conditions are imposed on you (whether by court order, agreement or
200
otherwise) that contradict the conditions of this License, they do not
201
excuse you from the conditions of this License.  If you cannot
202
distribute so as to satisfy simultaneously your obligations under this
203
License and any other pertinent obligations, then as a consequence you
204
may not distribute the Program at all.  For example, if a patent
205
license would not permit royalty-free redistribution of the Program by
206
all those who receive copies directly or indirectly through you, then
207
the only way you could satisfy both it and this License would be to
208
refrain entirely from distribution of the Program.
209

  
210
If any portion of this section is held invalid or unenforceable under
211
any particular circumstance, the balance of the section is intended to
212
apply and the section as a whole is intended to apply in other
213
circumstances.
214

  
215
It is not the purpose of this section to induce you to infringe any
216
patents or other property right claims or to contest validity of any
217
such claims; this section has the sole purpose of protecting the
218
integrity of the free software distribution system, which is
219
implemented by public license practices.  Many people have made
220
generous contributions to the wide range of software distributed
221
through that system in reliance on consistent application of that
222
system; it is up to the author/donor to decide if he or she is willing
223
to distribute software through any other system and a licensee cannot
224
impose that choice.
225

  
226
This section is intended to make thoroughly clear what is believed to
227
be a consequence of the rest of this License.
228

229
  8. If the distribution and/or use of the Program is restricted in
230
certain countries either by patents or by copyrighted interfaces, the
231
original copyright holder who places the Program under this License
232
may add an explicit geographical distribution limitation excluding
233
those countries, so that distribution is permitted only in or among
234
countries not thus excluded.  In such case, this License incorporates
235
the limitation as if written in the body of this License.
236

  
237
  9. The Free Software Foundation may publish revised and/or new versions
238
of the General Public License from time to time.  Such new versions will
239
be similar in spirit to the present version, but may differ in detail to
240
address new problems or concerns.
241

  
242
Each version is given a distinguishing version number.  If the Program
243
specifies a version number of this License which applies to it and "any
244
later version", you have the option of following the terms and conditions
245
either of that version or of any later version published by the Free
246
Software Foundation.  If the Program does not specify a version number of
247
this License, you may choose any version ever published by the Free Software
248
Foundation.
249

  
250
  10. If you wish to incorporate parts of the Program into other free
251
programs whose distribution conditions are different, write to the author
252
to ask for permission.  For software which is copyrighted by the Free
253
Software Foundation, write to the Free Software Foundation; we sometimes
254
make exceptions for this.  Our decision will be guided by the two goals
255
of preserving the free status of all derivatives of our free software and
256
of promoting the sharing and reuse of software generally.
257

  
258
			    NO WARRANTY
259

  
260
  11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
262
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
266
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
267
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268
REPAIR OR CORRECTION.
269

  
270
  12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278
POSSIBILITY OF SUCH DAMAGES.
279

  
280
		     END OF TERMS AND CONDITIONS
281

282
	    How to Apply These Terms to Your New Programs
283

  
284
  If you develop a new program, and you want it to be of the greatest
285
possible use to the public, the best way to achieve this is to make it
286
free software which everyone can redistribute and change under these terms.
287

  
288
  To do so, attach the following notices to the program.  It is safest
289
to attach them to the start of each source file to most effectively
290
convey the exclusion of warranty; and each file should have at least
291
the "copyright" line and a pointer to where the full notice is found.
292

  
293
    <one line to give the program's name and a brief idea of what it does.>
294
    Copyright (C) 19yy  <name of author>
295

  
296
    This program is free software; you can redistribute it and/or modify
297
    it under the terms of the GNU General Public License as published by
298
    the Free Software Foundation; either version 2 of the License, or
299
    (at your option) any later version.
300

  
301
    This program is distributed in the hope that it will be useful,
302
    but WITHOUT ANY WARRANTY; without even the implied warranty of
303
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
304
    GNU General Public License for more details.
305

  
306
    You should have received a copy of the GNU General Public License
307
    along with this program; if not, write to the Free Software
308
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
309

  
310
Also add information on how to contact you by electronic and paper mail.
311

  
312
If the program is interactive, make it output a short notice like this
313
when it starts in an interactive mode:
314

  
315
    Gnomovision version 69, Copyright (C) 19yy name of author
316
    Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
317
    This is free software, and you are welcome to redistribute it
318
    under certain conditions; type `show c' for details.
319

  
320
The hypothetical commands `show w' and `show c' should show the appropriate
321
parts of the General Public License.  Of course, the commands you use may
322
be called something other than `show w' and `show c'; they could even be
323
mouse-clicks or menu items--whatever suits your program.
324

  
325
You should also get your employer (if you work as a programmer) or your
326
school, if any, to sign a "copyright disclaimer" for the program, if
327
necessary.  Here is a sample; alter the names:
328

  
329
  Yoyodyne, Inc., hereby disclaims all copyright interest in the program
330
  `Gnomovision' (which makes passes at compilers) written by James Hacker.
331

  
332
  <signature of Ty Coon>, 1 April 1989
333
  Ty Coon, President of Vice
334

  
335
This General Public License does not permit incorporating your program into
336
proprietary programs.  If your program is a subroutine library, you may
337
consider it more useful to permit linking proprietary applications with the
338
library.  If this is what you want to do, use the GNU Library General
339
Public License instead of this License.
Makefile
1
CFLAGS= -O2 -Wall -g -I./libav
2
LDFLAGS= -g -L./libav
3

  
4
PROG= ffmpeg ffserver
5

  
6
all: lib $(PROG)
7

  
8
lib:
9
	make -C libav all
10

  
11
ffmpeg: rmenc.o mpegmux.o asfenc.o jpegenc.o swfenc.o udp.o formats.o grab.o ffmpeg.o libav/libav.a
12
	gcc $(LDFLAGS) -o $@ $^ -lav -lm
13

  
14
ffserver: rmenc.o mpegmux.o asfenc.o jpegenc.o swfenc.o formats.o grab.o ffserver.o libav/libav.a
15
	gcc $(LDFLAGS) -o $@ $^ -lav -lpthread -lm
16

  
17
%.o: %.c
18
	gcc $(CFLAGS) -c -o $@ $< 
19

  
20
clean: 
21
	make -C libav clean
22
	rm -f *.o *~ gmon.out TAGS $(PROG) 
23

  
24
etags:
25
	etags *.[ch] libav/*.[ch]
26

  
27
tar:
28
	(cd .. ; tar zcvf ffmpeg-0.3.tgz ffmpeg --exclude CVS --exclude TAGS )
README
1
FFmpeg version 0.9 - (c) 2000 Gerard Lantau.
2

  
3
1) Introduction
4
---------------
5

  
6
ffmpeg is a hyper fast realtime audio/video encoder and streaming
7
server. It can grab from a standard Video4Linux video source and
8
convert it into several file formats based on DCT/motion compensation
9
encoding. Sound is compressed in MPEG audio layer 2 or using an AC3
10
compatible stream.
11

  
12
What makes ffmpeg interesting ?
13

  
14
- Innovative streaming technology : multiformat, real time encoding,
15
  simple configuration.
16

  
17
- Simple and efficient video encoder: outputs MPEG1, H263 and Real
18
  Video(tm) compatible bitstreams using the same encoder core.
19

  
20
- Real time encoding (25 fps in 352x288 on a K6 500) using the video4linux API.
21

  
22
- Generates I and P frames, which means it is far better than a MJPEG
23
  encoder.
24

  
25
- Hyper fast MPEG audio layer 2 compression (50 times faster than
26
  realtime on a K6 500).
27

  
28
- Hyper fast AC3 compatible encoder.
29

  
30
- simple and very small portable C source code, easy to understand and
31
  to modify. It be may the smallest decent MPEG encoder :-)
32

  
33
ffmpeg is made of two programs:
34

  
35
* ffmpeg: soft VCR which encodes in real time to several formats.
36

  
37
* ffserver: live broadcast streaming server based on the ffmpeg core
38
encoders.
39

  
40
2) Documentation
41
----------------
42

  
43
read doc/ffmpeg.txt and doc/ffserver.txt to learn the basic features.
44

  
45
read ffmpeg
46

  
47
3) Licensing:
48
------------
49

  
50
* See the file COPYING. ffmpeg is licensed under the GNU General
51
  Public License.
52

  
53
* Source code from third parties: The DCT code comes from the Berkeley
54
  MPEG decoder and the JPEG encoder. 
55

  
56
* This code should be patent free since it is very simple. I took care
57
  to use the same video encoder core for all formats to show that they
58
  really ARE THE SAME except for the encoding huffman codes.
59

  
60
Gerard Lantau (glantau@users.sourceforge.net).
asfenc.c
1
/*
2
 * ASF compatible encoder
3
 * Copyright (c) 2000 Gerard Lantau.
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
 */
19
#include <stdlib.h>
20
#include <stdio.h>
21
#include <netinet/in.h>
22
#include <string.h>
23
#include "mpegenc.h"
24

  
25
#define PACKET_SIZE 3200
26
#define PACKET_HEADER_SIZE 12
27
#define FRAME_HEADER_SIZE 17
28

  
29
typedef struct {
30
    AVEncodeContext *enc;
31
    int num;
32
    int seq;
33
} ASFStream;
34

  
35
typedef struct {
36
    int is_streamed; /* true if streamed */
37
    int seqno;
38
    int packet_size;
39

  
40
    ASFStream streams[2];
41
    ASFStream *audio_stream, *video_stream;
42
    int nb_streams;
43
    /* non streamed additonnal info */
44
    int file_size_offset;
45
    int data_offset;
46
    /* packet filling */
47
    int packet_size_left;
48
    int packet_timestamp_start;
49
    int packet_timestamp_end;
50
    int packet_nb_frames;
51
    UINT8 packet_buf[PACKET_SIZE];
52
    PutByteContext pb;
53
} ASFContext;
54

  
55
typedef struct {
56
    UINT32 v1;
57
    UINT16 v2;
58
    UINT16 v3;
59
    UINT8 v4[8];
60
} GUID;
61

  
62
static const GUID asf_header = {
63
    0x75B22630, 0x668E, 0x11CF, { 0xA6, 0xD9, 0x00, 0xAA, 0x00, 0x62, 0xCE, 0x6C },
64
};
65

  
66
static const GUID file_header = {
67
    0x8CABDCA1, 0xA947, 0x11CF, { 0x8E, 0xE4, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 },
68
};
69

  
70
static const GUID stream_header = {
71
    0xB7DC0791, 0xA9B7, 0x11CF, { 0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65 },
72
};
73

  
74
static const GUID audio_stream = {
75
    0xF8699E40, 0x5B4D, 0x11CF, { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B },
76
};
77

  
78
static const GUID audio_conceal_none = {
79
    0x49f1a440, 0x4ece, 0x11d0, { 0xa3, 0xac, 0x00, 0xa0, 0xc9, 0x03, 0x48, 0xf6 },
80
};
81

  
82
static const GUID video_stream = {
83
    0xBC19EFC0, 0x5B4D, 0x11CF, { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B },
84
};
85

  
86
static const GUID video_conceal_none = {
87
    0x20FB5700, 0x5B55, 0x11CF, { 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B },
88
};
89

  
90
static const GUID comment_header = {
91
    0x86D15240, 0x311D, 0x11D0, { 0xA3, 0xA4, 0x00, 0xA0, 0xC9, 0x03, 0x48, 0xF6 },
92
};
93

  
94
static const GUID data_header = {
95
    0x75b22636, 0x668e, 0x11cf, { 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c },
96
};
97

  
98
static const GUID packet_guid = {
99
    0xF656CCE1, 0x03B3, 0x11D4, { 0xBE, 0xA2, 0x00, 0xA0, 0xCC, 0x3D, 0x72, 0x74 },
100
};
101
    
102
/* I am not a number !!! This GUID is the one found on the PC used to
103
   generate the stream */
104
static const GUID my_guid = {
105
    0x12345678, 0xA947, 0x11CF, { 0x31, 0x41, 0x59, 0x26, 0x20, 0x20, 0x20, 0x20 },
106
};
107

  
108
static void put_guid(PutByteContext *s, const GUID *g)
109
{
110
    int i;
111

  
112
    put_le32(s, g->v1);
113
    put_le16(s, g->v2);
114
    put_le16(s, g->v3);
115
    for(i=0;i<8;i++)
116
        put_byte(s, g->v4[i]);
117
}
118

  
119
#if 0
120
static void put_str16(PutByteContext *s, const char *tag)
121
{
122
    put_le16(s,strlen(tag));
123
    while (*tag) {
124
        put_le16(s, *tag++);
125
    }
126
}
127
#endif
128

  
129
/* write an asf chunk (only used in streaming case) */
130
static void put_chunk(AVFormatContext *s, int type, int payload_length)
131
{
132
    ASFContext *asf = s->priv_data;
133
    PutByteContext *pb = &s->pb;
134
    int length;
135

  
136
    length = payload_length + 8;
137
    put_le16(pb, type); 
138
    put_le16(pb, length); 
139
    put_le32(pb, asf->seqno);
140
    put_le16(pb, 0); /* unknown bytes */
141
    put_le16(pb, length);
142
    asf->seqno++;
143
}
144

  
145

  
146
static int asf_write_header(AVFormatContext *s)
147
{
148
    PutByteContext *pb = &s->pb;
149
    ASFContext *asf;
150
    int header_size, n, extra_size, extra_size2, i, wav_extra_size;
151
    AVEncodeContext *enc;
152
    long long header_offset, cur_pos;
153

  
154
    asf = malloc(sizeof(ASFContext));
155
    if (!asf)
156
        return -1;
157
    memset(asf, 0, sizeof(ASFContext));
158
    s->priv_data = asf;
159

  
160
    asf->packet_size = PACKET_SIZE;
161

  
162
    if (!s->is_streamed) {
163
        put_guid(pb, &asf_header);
164
        put_le64(pb, 0); /* header length, will be patched after */
165
        put_le32(pb, 6); /* ??? */
166
        put_byte(pb, 1); /* ??? */
167
        put_byte(pb, 2); /* ??? */
168
    } else {
169
        put_chunk(s, 0x4824, 0); /* start of stream (length will be patched later) */
170
    }
171
    
172
    /* file header */
173
    header_offset = put_pos(pb);
174
    put_guid(pb, &file_header);
175
    put_le64(pb, 24 + 80);
176
    put_guid(pb, &my_guid);
177
    asf->file_size_offset = put_pos(pb);
178
    put_le64(pb, 0); /* file size (patched after if not streamed) */
179
    put_le64(pb, 0); /* file time : 1601 :-) */
180
    put_le64(pb, 0x283); /* ??? */
181
    put_le64(pb, 0); /* stream 0 length in us */
182
    put_le64(pb, 0); /* stream 1 length in us */
183
    put_le32(pb, 0x0c1c); /* ??? */
184
    put_le32(pb, 0); /* ??? */
185
    put_le32(pb, 2); /* ??? */
186
    put_le32(pb, asf->packet_size); /* packet size */
187
    put_le32(pb, asf->packet_size); /* ??? */
188
    put_le32(pb, 0x03e800); /* ??? */
189
    
190
    /* stream headers */
191
    n = 0;
192
    for(i=0;i<2;i++) {
193
        if (i == 0)
194
            enc = s->audio_enc;
195
        else
196
            enc = s->video_enc;
197
        
198
        if (!enc)
199
            continue;
200
        asf->streams[n].num = i;
201
        asf->streams[n].seq = 0;
202
        asf->streams[n].enc = enc;
203
        
204
        switch(enc->codec->type) {
205
        case CODEC_TYPE_AUDIO:
206
            asf->audio_stream = &asf->streams[n];
207
            wav_extra_size = 0;
208
            extra_size = 18 + wav_extra_size;
209
            extra_size2 = 0;
210
            break;
211
        default:
212
        case CODEC_TYPE_VIDEO:
213
            asf->video_stream = &asf->streams[n];
214
            wav_extra_size = 0;
215
            extra_size = 0x33;
216
            extra_size2 = 0;
217
            break;
218
        }
219

  
220
        put_guid(pb, &stream_header);
221
        put_le64(pb, 24 + 16 * 2 + 22 + extra_size + extra_size2);
222
        if (enc->codec->type == CODEC_TYPE_AUDIO) {
223
            put_guid(pb, &audio_stream);
224
            put_guid(pb, &audio_conceal_none);
225
        } else {
226
            put_guid(pb, &video_stream);
227
            put_guid(pb, &video_conceal_none);
228
        }
229
        put_le64(pb, 0); /* ??? */
230
        put_le32(pb, extra_size); /* wav header len */
231
        put_le32(pb, extra_size2); /* additional data len */
232
        put_le16(pb, n + 1); /* stream number */
233
        put_le32(pb, 0); /* ??? */
234
        
235
        if (enc->codec->type == CODEC_TYPE_AUDIO) {
236
            /* WAVEFORMATEX header */
237

  
238
            put_le16(pb, 0x55); /* MP3 format */
239
            put_le16(pb, s->audio_enc->channels);
240
            put_le32(pb, s->audio_enc->rate);
241
            put_le32(pb, s->audio_enc->bit_rate / 8);
242
            put_le16(pb, 1); /* block align */
243
            put_le16(pb, 16); /* bits per sample */
244
            put_le16(pb, wav_extra_size);
245
        
246
            /* no addtionnal adata */
247
        } else {
248
            put_le32(pb, enc->width);
249
            put_le32(pb, enc->height);
250
            put_byte(pb, 2); /* ??? */
251
            put_le16(pb, 40); /* size */
252

  
253
            /* BITMAPINFOHEADER header */
254
            put_le32(pb, 40); /* size */
255
            put_le32(pb, enc->width);
256
            put_le32(pb, enc->height);
257
            put_le16(pb, 1); /* planes */
258
            put_le16(pb, 24); /* depth */
259
            /* compression type */
260
            switch(enc->codec->id) {
261
            case CODEC_ID_H263:
262
                put_tag(pb, "I263"); 
263
                break;
264
            case CODEC_ID_MJPEG:
265
                put_tag(pb, "MJPG");
266
                break;
267
            default:
268
                put_tag(pb, "XXXX");
269
                break;
270
            }
271
            put_le32(pb, enc->width * enc->height * 3);
272
            put_le32(pb, 0);
273
            put_le32(pb, 0);
274
            put_le32(pb, 0);
275
            put_le32(pb, 0);
276
        }
277
        n++;
278
    }
279
    asf->nb_streams = n;
280

  
281
    /* XXX: should media comments */
282
#if 0
283
    put_guid(pb, &comment_header);
284
    put_le64(pb, 0);
285
#endif
286
    /* patch the header size fields */
287
    cur_pos = put_pos(pb);
288
    header_size = cur_pos - header_offset;
289
    if (!s->is_streamed) {
290
        header_size += 24 + 6;
291
        put_seek(pb, header_offset - 14, SEEK_SET);
292
        put_le64(pb, header_size);
293
    } else {
294
        header_size += 8 + 50;
295
        put_seek(pb, header_offset - 10, SEEK_SET);
296
        put_le16(pb, header_size);
297
        put_seek(pb, header_offset - 2, SEEK_SET);
298
        put_le16(pb, header_size);
299
    }
300
    put_seek(pb, cur_pos, SEEK_SET);
301
    
302
    /* movie chunk, followed by packets of packet_size */
303
    asf->data_offset = cur_pos;
304
    put_guid(pb, &data_header);
305
    put_le64(pb, 24); /* will be patched after */
306
    put_guid(pb, &packet_guid);
307
    put_le64(pb, 0x283); /* ??? */
308
    put_byte(pb, 1); /* ??? */
309
    put_byte(pb, 1); /* ??? */
310

  
311
    put_flush_packet(&s->pb);
312

  
313
    asf->packet_nb_frames = 0;
314
    asf->packet_timestamp_start = -1;
315
    asf->packet_timestamp_end = -1;
316
    asf->packet_size_left = asf->packet_size - PACKET_HEADER_SIZE;
317
    init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size,
318
                  NULL, NULL, NULL);
319

  
320
    return 0;
321
}
322

  
323
/* write a fixed size packet */
324
static void put_packet(AVFormatContext *s, 
325
                       unsigned int timestamp, unsigned int duration, 
326
                       int nb_frames, int padsize)
327
{
328
    ASFContext *asf = s->priv_data;
329
    PutByteContext *pb = &s->pb;
330
    int flags;
331

  
332
    if (s->is_streamed) {
333
        put_chunk(s, 0x4424, asf->packet_size);
334
    }
335

  
336
    put_byte(pb, 0x82);
337
    put_le16(pb, 0);
338
    
339
    flags = 0x01; /* nb segments present */
340
    if (padsize > 0) {
341
        if (padsize < 256)
342
            flags |= 0x08;
343
        else
344
            flags |= 0x10;
345
    }
346
    put_byte(pb, flags); /* flags */
347
    put_byte(pb, 0x5d);
348
    if (flags & 0x10)
349
        put_le16(pb, padsize);
350
    if (flags & 0x08)
351
        put_byte(pb, padsize);
352
    put_le32(pb, timestamp);
353
    put_le16(pb, duration);
354
    put_byte(pb, nb_frames | 0x80);
355
}
356

  
357
static void flush_packet(AVFormatContext *s)
358
{
359
    ASFContext *asf = s->priv_data;
360
    int hdr_size, ptr;
361
    
362
    put_packet(s, asf->packet_timestamp_start, 
363
               asf->packet_timestamp_end - asf->packet_timestamp_start, 
364
               asf->packet_nb_frames, asf->packet_size_left);
365
    
366
    /* compute padding */
367
    hdr_size = PACKET_HEADER_SIZE;
368
    if (asf->packet_size_left > 0) {
369
        /* if padding needed, don't forget to count the
370
           padding byte in the header size */
371
        hdr_size++;
372
        asf->packet_size_left--;
373
        /* XXX: I do not test again exact limit to avoid boundary problems */
374
        if (asf->packet_size_left > 200) {
375
            hdr_size++;
376
            asf->packet_size_left--;
377
        }
378
    }
379
    ptr = asf->packet_size - PACKET_HEADER_SIZE - asf->packet_size_left;
380
    memset(asf->packet_buf + ptr, 0, asf->packet_size_left);
381
    
382
    put_buffer(&s->pb, asf->packet_buf, asf->packet_size - hdr_size);
383
    
384
    put_flush_packet(&s->pb);
385
    asf->packet_nb_frames = 0;
386
    asf->packet_timestamp_start = -1;
387
    asf->packet_timestamp_end = -1;
388
    asf->packet_size_left = asf->packet_size - PACKET_HEADER_SIZE;
389
    init_put_byte(&asf->pb, asf->packet_buf, asf->packet_size,
390
                  NULL, NULL, NULL);
391
}
392

  
393
static void put_frame_header(AVFormatContext *s, ASFStream *stream, int timestamp,
394
                             int payload_size, int frag_offset, int frag_len)
395
{
396
    ASFContext *asf = s->priv_data;
397
    PutByteContext *pb = &asf->pb;
398
    int val;
399

  
400
    val = stream->num;
401
    if (stream->enc->key_frame)
402
        val |= 0x80;
403
    put_byte(pb, val);
404
    put_byte(pb, stream->seq);
405
    put_le32(pb, frag_offset); /* fragment offset */
406
    put_byte(pb, 0x08); /* flags */
407
    put_le32(pb, payload_size);
408
    put_le32(pb, timestamp);
409
    put_le16(pb, frag_len);
410
}
411

  
412

  
413
/* Output a frame. We suppose that payload_size <= PACKET_SIZE.
414

  
415
   It is there that you understand that the ASF format is really
416
   crap. They have misread the MPEG Systems spec ! 
417
 */
418
static void put_frame(AVFormatContext *s, ASFStream *stream, int timestamp,
419
                      UINT8 *buf, int payload_size)
420
{
421
    ASFContext *asf = s->priv_data;
422
    int frag_pos, frag_len, frag_len1;
423
    
424
    frag_pos = 0;
425
    while (frag_pos < payload_size) {
426
        frag_len = payload_size - frag_pos;
427
        frag_len1 = asf->packet_size_left - FRAME_HEADER_SIZE;
428
        if (frag_len1 > 0) {
429
            if (frag_len > frag_len1)
430
                frag_len = frag_len1;
431
            put_frame_header(s, stream, timestamp, payload_size, frag_pos, frag_len);
432
            put_buffer(&asf->pb, buf, frag_len);
433
            asf->packet_size_left -= (frag_len + FRAME_HEADER_SIZE);
434
            asf->packet_timestamp_end = timestamp;
435
            if (asf->packet_timestamp_start == -1)
436
                asf->packet_timestamp_start = timestamp;
437
            asf->packet_nb_frames++;
438
        } else {
439
            frag_len = 0;
440
        }
441
        frag_pos += frag_len;
442
        buf += frag_len;
443
        /* output the frame if filled */
444
        if (asf->packet_size_left <= FRAME_HEADER_SIZE)
445
            flush_packet(s);
446
    }
447
    stream->seq++;
448
}
449

  
450

  
451
static int asf_write_audio(AVFormatContext *s, UINT8 *buf, int size)
452
{
453
    ASFContext *asf = s->priv_data;
454
    int timestamp;
455

  
456
    timestamp = (int)((float)s->audio_enc->frame_number * s->audio_enc->frame_size * 1000.0 / 
457
                      s->audio_enc->rate);
458
    put_frame(s, asf->audio_stream, timestamp, buf, size);
459
    return 0;
460
}
461

  
462
static int asf_write_video(AVFormatContext *s, UINT8 *buf, int size)
463
{
464
    ASFContext *asf = s->priv_data;
465
    int timestamp;
466

  
467
    timestamp = (int)((float)s->video_enc->frame_number * 1000.0 / 
468
                      s->video_enc->rate);
469
    put_frame(s, asf->audio_stream, timestamp, buf, size);
470
    return 0;
471
}
472

  
473
static int asf_write_trailer(AVFormatContext *s)
474
{
475
    ASFContext *asf = s->priv_data;
476
    long long file_size;
477

  
478
    /* flush the current packet */
479
    if (asf->pb.buf_ptr > asf->pb.buffer)
480
        flush_packet(s);
481

  
482
    if (s->is_streamed) {
483
        put_chunk(s, 0x4524, 0); /* end of stream */
484
    } else {
485
        /* patch the various place which depends on the file size */
486
        file_size = put_pos(&s->pb);
487
        put_seek(&s->pb, asf->file_size_offset, SEEK_SET);
488
        put_le64(&s->pb, file_size);
489
        put_seek(&s->pb, asf->data_offset + 16, SEEK_SET);
490
        put_le64(&s->pb, file_size - asf->data_offset);
491
    }
492

  
493
    put_flush_packet(&s->pb);
494

  
495
    free(asf);
496
    return 0;
497
}
498

  
499
AVFormat asf_format = {
500
    "asf",
501
    "asf format",
502
    "application/octet-stream",
503
    "asf",
504
    CODEC_ID_MP2,
505
    CODEC_ID_MJPEG,
506
    asf_write_header,
507
    asf_write_audio,
508
    asf_write_video,
509
    asf_write_trailer,
510
};
doc/BUGS
1
- Sound is only handled in mono. The fixed psycho acoustic model is
2
  very bad, although the quality is surpringly good for such a model !
3

  
4
- the bit rate control is really not correct. 
5

  
6
- Only frame size multiple of 16 are handled.
7

  
8
- If you want a specific format to be added (I am thinking about
9
  MJPEG, H261) please tell me. Of course, the format you choose should
10
  be based on MPEG to be easily integrated
11

  
12
- ffmpeg can be used to generate streaming audio/video on a
13
  server. Please tell me if you are interested.
14

  
doc/README.tech
1
Technical notes:
2
---------------
3

  
4
- To increase speed, only motion vectors (0,0) are tested
5

  
6
- The decision intra/predicted macroblock is the algorithm suggested
7
  by the mpeg 1 specification.
8

  
9
- only Huffman based H263 is supported, mainly because of patent
10
  issues.
11

  
12
- Many options can be modified only in the source code.
13

  
14
- I rewrote the mpeg audio layer 2 compatible encoder from scratch. It
15
  is one of the simplest encoder you can imagine (800 lines of C code
16
  !). It is also one of the fastest because of its simplicity. They
17
  are still some problems of overflow. A minimal psycho acoustic model
18
  could be added. Only mono stream can be generated. I may add stereo
19
  if needed.
20

  
21
- I rewrote the AC3 audio encoder from scratch. It is fairly naive,
22
  but the result are quiet interesting at 64 kbit/s. It includes
23
  extensions for low sampling rates used in some Internet
24
  formats. Differential and coupled stereo is not handled. Stereo
25
  channels are simply handled as two mono channels.
doc/TODO
1
ffmpeg TODO list:
2

  
3
- Add ASF format.
4

  
5
- add MJPEG codec.
6

  
7
- fix skip frame bug in mpeg video
8

  
9
- fix G2 audio problem (bad volume in AC3 ?)
10

  
11
- use non zero motion vectors.
12

  
13
- test fifo & master handling
14

  
15
- deny & allow + password.
16

  
17
- Improve psycho acoustic model for AC3 & mpeg audio.
18

  
19
- Improve the bit rate control for video codecs.
doc/ffmpeg.txt
1
* ffmpeg can use YUV files as input : 
2

  
3
  ffmpeg /tmp/out.mpg /tmp/test
4

  
5
If will use the files: 
6
/tmp/test0.Y, /tmp/test0.U, /tmp/test0.V, 
7
/tmp/test1.Y, /tmp/test1.U, /tmp/test1.V, etc...
8

  
9
The Y files use twice the resolution of the U and V files. They are
10
raw files, without header. They can be generated by all decent video
11
decoders.
12

  
13
* ffmpeg can use a video4linux compatible video source : 
14

  
15
  ffmpeg /tmp/out.mpg 
16

  
17
  Note that you must activate the right video source and channel
18
  before launching ffmpeg. You can use any TV viewer such as xawtv by
19
  Gerd Knorr which I find very good.
20

  
21
* There are some importants options to know:
22

  
23
-s size      set frame size                [160x128]
24
-f format    set encoding format           [mpeg1]
25
-r fps       set frame rate                [25]
26
-b bitrate   set the bitrate in kbit/s     [100]
27
-t time      set recording time in seconds [10.0]
28

  
29
The frame size can also be: cif, qcif, sqcif and 4cif.
30
The encoding format can be mpeg1, h263 or rv10.
31

  
32
Advanced options are:
33

  
34
-d device    set video4linux device name
35
-g gop_size  set the group of picture size. 
36
             An 'intra' frame is generated every gop_size frames.
37
-i           use only intra images (speed up compression, but lower quality).
38
-c comment   set the comment string.
39

  
40
Comment strings are only used for Real Video(tm) encoding. Tags are
41
used in the comment string. A typical comment string is:
42

  
43
"+title=Test Video +author=FFMpeg +copyright=Free +comment=Generated by FFMpeg 1.0"
44

  
45
The output file can be "-" to output to a pipe. This is only possible
46
with mpeg1 and h263 formats. 
47

  
48
* Tips:
49

  
50
- For low bit rate application, use a low frame rate and a small gop
51
  size. This is especially true for real video where the Linux player
52
  does not seem to be very fast, so it can miss frames. An example is:
53

  
54
  ffmpeg -g 3 -r 3 -t 10 -b 50 -s qcif -f rv10 /tmp/b.rm
55

  
56
- The parameter 'q' which is displayed while encoding is the current
57
  quantizer. The value of 1 indicates that a very good quality could
58
  be achieved. The value of 31 indicates the worst quality. If q=31
59
  too often, it means that the encoder cannot compress enough to meet
60
  your bit rate. You must either increase the bit rate, decrease the
61
  frame rate or decrease the frame size.
62

  
doc/ffserver.conf
1
# Port on which the server is listening. You must select a different
2
# port from your standard http web server if it is running on the same
3
# computer.
4

  
5
Port 8080
6

  
7
# Address on which the server is bound. Only useful if you have
8
# several network interfaces.
9

  
10
BindAddress 0.0.0.0
11

  
12
# Host and port of the master server if you which that this server
13
# duplicates another existing server. Otherwise, the server does the
14
# audio/video grab itself. See the following options for the grab parameters 
15

  
16
#MasterServer http://localhost:80/index.html
17

  
18
# Grab parameters
19

  
20
#AudioDevice /dev/dsp
21
#VideoDevice /dev/video
22

  
23
# Number of simultaneous requests that can be handled. Since FFServer
24
# is very fast, this limit is determined mainly by your Internet
25
# connection speed.
26

  
27
MaxClients 1000
28

  
29
# Access Log file (uses standard Apache log file format)
30
# '-' is the standard output
31

  
32
CustomLog -
33

  
34
##################################################################
35
# Now you can define each stream which will be generated from the
36
# original audio and video stream. Each format has a filename (here
37
# 'test128.mpg'). FFServer will send this stream when answering a
38
# request containing this filename.
39

  
40
<Stream test1.mpg>
41

  
42
# Format of the stream : you can choose among:
43
# mpeg1      : MPEG1 multiplexed video and audio
44
# mpeg1video : only MPEG1 video
45
# mp2        : MPEG audio layer 2
46
# mp3        : MPEG audio layer 3 (currently sent as layer 2)
47
# rm         : Real Networks compatible stream. Multiplexed audio and video.
48
# ra         : Real Networks compatible stream. Audio only.
49
# mpjpeg     : Multipart JPEG (works with Netscape without any plugin)
50
# jpeg       : Generate a single JPEG image.
51
# asf        : ASF compatible stream (Windows Media Player format)
52
# swf        : Macromedia flash(tm) compatible stream
53
# master     : special ffmpeg stream used to duplicate a server
54

  
55
Format mpeg1
56

  
57
# Bitrate for the audio stream. Codecs usually support only a few different bitrates. 
58

  
59
AudioBitRate 32
60

  
61
# Number of audio channels : 1 = mono, 2 = stereo
62

  
63
AudioChannels 1
64

  
65
# Sampling frequency for audio. When using low bitrates, you should
66
# lower this frequency to 22050 or 11025. The supported frequencies
67
# depend on the selected audio codec.
68

  
69
AudioSampleRate 44100
70

  
71
# Bitrate for the video stream.
72
VideoBitRate 64
73

  
74
# Number of frames per second
75
VideoFrameRate 3
76

  
77
# Size of the video frame : WxH
78
# W : width, H : height
79
# The following abbreviation are defined : sqcif, qcif, cif, 4cif
80
#VideoSize    352x240
81

  
82
# transmit only intra frames (useful for low bitrates) 
83
VideoIntraOnly
84

  
85
# If non intra only, an intra frame is transmitted every VideoGopSize
86
# frames Video synchronization can only begin at an I frames.
87
#VideoGopSize 12
88

  
89
</Stream>
90

  
91
# second mpeg stream with high frame rate
92

  
93
<Stream test2.mpg>
94
Format mpeg1video
95
VideoBitRate 128
96
VideoFrameRate 25
97
#VideoSize    352x240
98
VideoGopSize 25
99
</Stream>
100

  
101
##################################################################
102
# Another stream : used to download data to another server which
103
# duplicates this one
104

  
105
<Stream master>
106

  
107
Format master
108

  
109
</Stream>
110

  
111
##################################################################
112
# Another stream : Real with audio only at 32 kbits
113

  
114
<Stream test.ra>
115

  
116
Format ra
117
AudioBitRate 32
118

  
119
</Stream>
120

  
121
##################################################################
122
# Another stream : Real with audio and video at 64 kbits
123

  
124
<Stream test.rm>
125

  
126
Format rm
127

  
128
AudioBitRate 32
129
VideoBitRate 20
130
VideoFrameRate 2
131
VideoIntraOnly
132

  
133
</Stream>
134

  
135
##################################################################
136
# Another stream : Mpeg audio layer 2 at 64 kbits.
137

  
138
<Stream test.mp2>
139

  
140
Format mp2
141
AudioBitRate 64
142
AudioSampleRate 44100
143

  
144
</Stream>
145

  
146
<Stream test1.mp2>
147

  
148
Format mp2
149
AudioBitRate 32
150
AudioSampleRate 16000
151

  
152
</Stream>
153

  
154
##################################################################
155
# Another stream : Multipart JPEG
156

  
157
<Stream test.mjpg>
158

  
159
Format mpjpeg
160

  
161
VideoFrameRate 2
162
VideoIntraOnly
163

  
164
</Stream>
165

  
166
##################################################################
167
# Another stream : Multipart JPEG
168

  
169
<Stream test.jpg>
170

  
171
Format jpeg
172

  
173
# the parameters are choose here to take the same output as the
174
# Multipart JPEG one.
175
VideoFrameRate 2 
176
VideoIntraOnly
177

  
178
</Stream>
179

  
180
##################################################################
181
# Another stream : Flash
182

  
183
<Stream test.swf>
184

  
185
Format swf
186

  
187
VideoFrameRate 2
188
VideoIntraOnly
189

  
190
</Stream>
191

  
192

  
193
##################################################################
194
# Another stream : ASF compatible
195

  
196
<Stream test.asf>
197

  
198
Format asf
199

  
200
AudioBitRate 64
201
AudioSampleRate 44100
202
VideoFrameRate 2
203
VideoIntraOnly
204

  
205
</Stream>
206

  
207
##################################################################
208
# Another stream : server status
209

  
210
<Stream stat.html>
211

  
212
Format status
213

  
214
</Stream>
ffmpeg.c
1
/*
2
 * Basic user interface for ffmpeg system
3
 * Copyright (c) 2000 Gerard Lantau
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
 */
19
#include <stdlib.h>
20
#include <stdio.h>
21
#include <netinet/in.h>
22
#include <linux/videodev.h>
23
#include <linux/soundcard.h>
24
#include <unistd.h>
25
#include <fcntl.h>
26
#include <sys/ioctl.h>
27
#include <sys/mman.h>
28
#include <errno.h>
29
#include <sys/time.h>
30
#include <getopt.h>
31

  
32
#include "mpegenc.h"
33
#include "mpegvideo.h"
34

  
35
static AVFormat *file_format;
36
static int frame_width  = 160;
37
static int frame_height = 128;
38
static int frame_rate = 25;
39
static int bit_rate = 200000;
40
static int video_disable = 0;
41

  
42
static const char *video_filename, *audio_filename;
43
static float recording_time = 10.0;
44
static int nb_frames;
45
static int gop_size = 12;
46
static int intra_only = 0;
47
static int audio_freq = 44100;
48
static int audio_bit_rate = 64000;
49
static int audio_disable = 0;
50
static int audio_channels = 1;
51

  
52
static long long time_start;
53

  
54

  
55
static int file_read_picture(AVEncodeContext *s, 
56
                             UINT8 *picture[3],
57
                             int width, int height,
58
                             int picture_number)
59
{
60
    FILE *f;
61
    char buf[1024];
62
    static int init = 0;
63
    static UINT8 *pict[3];
64
    if (!init) {
65
        pict[0] = malloc(width * height);
66
        pict[1] = malloc(width * height / 4);
67
        pict[2] = malloc(width * height / 4);
68
        init = 1;
69
    }
70
    
71
    picture[0] = pict[0];
72
    picture[1] = pict[1];
73
    picture[2] = pict[2];
74

  
75
    sprintf(buf, "%s%d.Y", video_filename, picture_number);
76
    f=fopen(buf, "r");
77
    if (!f) {
78
        return -1;
79
    }
80

  
81
    fread(picture[0], 1, width * height, f);
82
    fclose(f);
83

  
84
    sprintf(buf, "%s%d.U", video_filename, picture_number);
85
    f=fopen(buf, "r");
86
    if (!f) {
87
        perror(buf);
88
        exit(1);
89
    }
90
    fread(picture[1], 1, width * height / 4, f);
91
    fclose(f);
92
    
93
    sprintf(buf, "%s%d.V", video_filename, picture_number);
94
    f=fopen(buf, "r");
95
    if (!f) {
96
        perror(buf);
97
        exit(1);
98
    }
99
    fread(picture[2], 1, width * height / 4, f);
100
    fclose(f);
101
    return 0;
102
}
103

  
104
static void display_stats(AVEncodeContext *video_ctx, 
105
                          AVEncodeContext *audio_ctx,
106
                          int batch_mode, int the_end)
107
{
108
    if (video_ctx && 
109
        ((video_ctx->frame_number % video_ctx->rate) == 0 || 
110
        the_end)) {
111
        float ti;
112
        
113
        if (batch_mode) {
114
            ti = (float)video_ctx->frame_number / video_ctx->rate;
115
        } else {
116
            ti = (gettime() - time_start) / 1000000.0;
117
            if (ti < 0.1)
118
                    ti = 0.1;
119
        }
120
        
121
        fprintf(stderr,
122
                "frame=%5d size=%8dkB time=%0.1f fps=%4.1f bitrate=%6.1fkbits/s q=%2d\r", 
123
                video_ctx->frame_number,
124
                data_out_size / 1024,
125
                ti,
126
                video_ctx->frame_number / ti,
127
                (data_out_size * 8 / ti / 1000),
128
                ((MpegEncContext *)video_ctx->priv_data)->qscale);
129
        if (the_end) {
130
            fprintf(stderr,"\n");
131
        }
132
        fflush(stderr);
133
    }
134
#if 0
135
    if (the_end && batch_mode && audio_ctx) {
136
        duration = (gettime() - ti) / 1000000.0;
137
        factor = 0;
138
        if (ti > 0) {
139
            factor = (float)nb_samples / s->sample_rate / duration;
140
        }
141
        fprintf(stderr, "%0.1f seconds compressed in %0.1f seconds (speed factor: %0.1f)\n",
142
                (float)nb_samples / s->sample_rate, 
143
                duration,
144
                factor);
145
    }
146
#endif
147
}
148

  
149
void raw_write_data(void *opaque, 
150
                    unsigned char *buf, int size)
151
{
152
    FILE *outfile = opaque;
153
    fwrite(buf, 1, size, outfile);
154
    data_out_size += size;
155
}
156

  
157
int raw_seek(void *opaque, long long offset, int whence)
158
{
159
    FILE *outfile = opaque;
160
    fseek(outfile, offset, whence);
161
    return 0;
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff