annotate yuv4mpeg.c @ 355:46029c682234 libavformat

seeking stuff adaptively change middle position selection algo for seeking, this avoids some ugly worstcases of the interpolated variant avoid backward search for mpeg where possible, its 17 times slower then forward according to my benchmark
author michael
date Sat, 17 Jan 2004 20:26:44 +0000
parents 981b0b3c95dd
children 845f9de2c883
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
1 /*
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
2 * YUV4MPEG format
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
3 * Copyright (c) 2001, 2002, 2003 Fabrice Bellard.
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
4 *
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
8 * version 2 of the License, or (at your option) any later version.
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
9 *
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
13 * Lesser General Public License for more details.
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
14 *
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
18 */
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
19 #include "avformat.h"
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
20
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
21 #define Y4M_MAGIC "YUV4MPEG2"
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
22 #define Y4M_FRAME_MAGIC "FRAME"
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
23 #define Y4M_LINE_MAX 256
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
24
277
a313e1080322 disable encoders where appropriate (patch courtesy of BERO
melanson
parents: 256
diff changeset
25 #ifdef CONFIG_ENCODERS
284
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
26
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
27 static int yuv4_generate_header(AVFormatContext *s, char* buf)
18
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
28 {
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
29 AVStream *st;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
30 int width, height;
288
981b0b3c95dd AVRational
michael
parents: 287
diff changeset
31 int raten, rated, aspectn, aspectd, n;
284
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
32 char inter;
18
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
33
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
34 st = s->streams[0];
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
35 width = st->codec.width;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
36 height = st->codec.height;
85
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 65
diff changeset
37
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 65
diff changeset
38 av_reduce(&raten, &rated, st->codec.frame_rate, st->codec.frame_rate_base, (1UL<<31)-1);
284
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
39
288
981b0b3c95dd AVRational
michael
parents: 287
diff changeset
40 aspectn = st->codec.sample_aspect_ratio.num;
981b0b3c95dd AVRational
michael
parents: 287
diff changeset
41 aspectd = st->codec.sample_aspect_ratio.den;
18
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
42
284
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
43 inter = 'p'; /* progressive is the default */
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
44 if (st->codec.coded_frame && st->codec.coded_frame->interlaced_frame) {
287
0b3ec5cba845 bottom_field_first -> top_field_first
michael
parents: 284
diff changeset
45 inter = st->codec.coded_frame->top_field_first ? 't' : 'b';
284
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
46 }
18
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
47
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
48 /* construct stream header, if this is the first frame */
284
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
49 n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
18
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
50 Y4M_MAGIC,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
51 width,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
52 height,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
53 raten, rated,
284
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
54 inter,
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
55 aspectn, aspectd,
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
56 (st->codec.pix_fmt == PIX_FMT_YUV411P) ? " XYSCSS=411" : "");
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
57
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
58 return n;
18
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
59 }
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
60
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
61 static int yuv4_write_packet(AVFormatContext *s, int stream_index,
241
3d92f793fd67 64 bit pts for writing - more const usage
bellard
parents: 207
diff changeset
62 const uint8_t *buf, int size, int64_t pts)
18
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
63 {
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
64 AVStream *st = s->streams[stream_index];
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
65 ByteIOContext *pb = &s->pb;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
66 AVPicture *picture;
284
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
67 int* first_pkt = s->priv_data;
18
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
68 int width, height;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
69 int i, m;
284
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
70 char buf2[Y4M_LINE_MAX+1];
18
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
71 char buf1[20];
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 18
diff changeset
72 uint8_t *ptr, *ptr1, *ptr2;
18
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
73
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
74 picture = (AVPicture *)buf;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
75
284
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
76 /* for the first packet we have to output the header as well */
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
77 if (*first_pkt) {
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
78 *first_pkt = 0;
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
79 if (yuv4_generate_header(s, buf2) < 0) {
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
80 fprintf(stderr, "Error. YUV4MPEG stream header write failed.\n");
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
81 return -EIO;
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
82 } else {
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
83 put_buffer(pb, buf2, strlen(buf2));
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
84 }
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
85 }
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
86
18
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
87 /* construct frame header */
284
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
88
256
2efd6fe95fc6 yuv4mpeg.c extra space patch by ("Steven M. Schultz" <sms at 2BSD dot COM>)
michaelni
parents: 241
diff changeset
89 m = snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
18
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
90 put_buffer(pb, buf1, strlen(buf1));
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
91
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
92 width = st->codec.width;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
93 height = st->codec.height;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
94
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
95 ptr = picture->data[0];
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
96 for(i=0;i<height;i++) {
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
97 put_buffer(pb, ptr, width);
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
98 ptr += picture->linesize[0];
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
99 }
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
100
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
101 height >>= 1;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
102 width >>= 1;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
103 ptr1 = picture->data[1];
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
104 ptr2 = picture->data[2];
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
105 for(i=0;i<height;i++) { /* Cb */
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
106 put_buffer(pb, ptr1, width);
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
107 ptr1 += picture->linesize[1];
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
108 }
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
109 for(i=0;i<height;i++) { /* Cr */
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
110 put_buffer(pb, ptr2, width);
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
111 ptr2 += picture->linesize[2];
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
112 }
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
113 put_flush_packet(pb);
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
114 return 0;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
115 }
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
116
284
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
117 static int yuv4_write_header(AVFormatContext *s)
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
118 {
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
119 int* first_pkt = s->priv_data;
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
120
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
121 if (s->nb_streams != 1)
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
122 return -EIO;
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
123
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
124 if (s->streams[0]->codec.pix_fmt == PIX_FMT_YUV411P) {
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
125 fprintf(stderr, "Warning: generating non-standard 4:1:1 YUV stream, some mjpegtools might not work.\n");
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
126 }
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
127 else if (s->streams[0]->codec.pix_fmt != PIX_FMT_YUV420P) {
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
128 fprintf(stderr, "ERROR: yuv4mpeg only handles 4:2:0, 4:1:1 YUV data. Use -pix_fmt to select one.\n");
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
129 return -EIO;
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
130 }
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
131
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
132 *first_pkt = 1;
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
133 return 0;
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
134 }
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
135
18
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
136 static int yuv4_write_trailer(AVFormatContext *s)
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
137 {
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
138 return 0;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
139 }
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
140
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
141 AVOutputFormat yuv4mpegpipe_oformat = {
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
142 "yuv4mpegpipe",
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
143 "YUV4MPEG pipe format",
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
144 "",
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
145 "yuv4mpeg",
284
c77ce17451a1 * providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents: 277
diff changeset
146 sizeof(int),
18
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
147 CODEC_ID_NONE,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
148 CODEC_ID_RAWVIDEO,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
149 yuv4_write_header,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
150 yuv4_write_packet,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
151 yuv4_write_trailer,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
152 .flags = AVFMT_RAWPICTURE,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
153 };
277
a313e1080322 disable encoders where appropriate (patch courtesy of BERO
melanson
parents: 256
diff changeset
154 #endif //CONFIG_ENCODERS
18
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
155
207
7865656658dc stdin patch by (Charles Yates <charles dot yates at pandora dot be>)
michaelni
parents: 184
diff changeset
156 /* Header size increased to allow room for optional flags */
7865656658dc stdin patch by (Charles Yates <charles dot yates at pandora dot be>)
michaelni
parents: 184
diff changeset
157 #define MAX_YUV4_HEADER 80
184
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
158 #define MAX_FRAME_HEADER 10
18
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
159
184
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
160 static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap)
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
161 {
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
162 char header[MAX_YUV4_HEADER+1];
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
163 int i;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
164 ByteIOContext *pb = &s->pb;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
165 int width, height, raten, rated, aspectn, aspectd;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
166 char lacing;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
167 AVStream *st;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
168
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
169 for (i=0; i<MAX_YUV4_HEADER; i++) {
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
170 header[i] = get_byte(pb);
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
171 if (header[i] == '\n') {
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
172 header[i+1] = 0;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
173 break;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
174 }
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
175 }
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
176 if (i == MAX_YUV4_HEADER) return -1;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
177 if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) return -1;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
178 sscanf(header+strlen(Y4M_MAGIC), " W%d H%d F%d:%d I%c A%d:%d",
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
179 &width, &height, &raten, &rated, &lacing, &aspectn, &aspectd);
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
180
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
181 st = av_new_stream(s, 0);
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
182 st = s->streams[0];
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
183 st->codec.width = width;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
184 st->codec.height = height;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
185 av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1);
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
186 st->codec.frame_rate = raten;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
187 st->codec.frame_rate_base = rated;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
188 st->codec.pix_fmt = PIX_FMT_YUV420P;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
189 st->codec.codec_type = CODEC_TYPE_VIDEO;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
190 st->codec.codec_id = CODEC_ID_RAWVIDEO;
288
981b0b3c95dd AVRational
michael
parents: 287
diff changeset
191 st->codec.sample_aspect_ratio= (AVRational){aspectn, aspectd};
184
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
192
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
193 return 0;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
194 }
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
195
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
196 static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
197 {
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
198 int i;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
199 char header[MAX_FRAME_HEADER+1];
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
200 int packet_size, ret, width, height;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
201 AVStream *st = s->streams[0];
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
202
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
203 for (i=0; i<MAX_FRAME_HEADER; i++) {
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
204 header[i] = get_byte(&s->pb);
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
205 if (header[i] == '\n') {
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
206 header[i+1] = 0;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
207 break;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
208 }
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
209 }
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
210 if (i == MAX_FRAME_HEADER) return -1;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
211 if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC))) return -1;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
212
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
213 width = st->codec.width;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
214 height = st->codec.height;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
215
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
216 packet_size = avpicture_get_size(st->codec.pix_fmt, width, height);
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
217 if (packet_size < 0)
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
218 av_abort();
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
219
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
220 if (av_new_packet(pkt, packet_size) < 0)
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
221 return -EIO;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
222
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
223 pkt->stream_index = 0;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
224 ret = get_buffer(&s->pb, pkt->data, pkt->size);
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
225 if (ret != pkt->size) {
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
226 av_free_packet(pkt);
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
227 return -EIO;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
228 } else {
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
229 return 0;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
230 }
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
231 }
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
232
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
233 static int yuv4_read_close(AVFormatContext *s)
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
234 {
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
235 return 0;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
236 }
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
237
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
238 AVInputFormat yuv4mpegpipe_iformat = {
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
239 "yuv4mpegpipe",
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
240 "YUV4MPEG pipe format",
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
241 0,
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
242 NULL,
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
243 yuv4_read_header,
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
244 yuv4_read_packet,
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
245 yuv4_read_close,
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
246 .extensions = "yuv4mpeg"
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
247 };
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
248
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
249 int yuv4mpeg_init(void)
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
250 {
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
251 av_register_input_format(&yuv4mpegpipe_iformat);
277
a313e1080322 disable encoders where appropriate (patch courtesy of BERO
melanson
parents: 256
diff changeset
252 #ifdef CONFIG_ENCODERS
184
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
253 av_register_output_format(&yuv4mpegpipe_oformat);
277
a313e1080322 disable encoders where appropriate (patch courtesy of BERO
melanson
parents: 256
diff changeset
254 #endif //CONFIG_ENCODERS
184
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
255 return 0;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
256 }
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
257