av_log() patch by (Michel Bardiaux <mbardiaux at peaktime dot be>)
[libav.git] / libavformat / yuv4mpeg.c
CommitLineData
0b9fb89d
FB
1/*
2 * YUV4MPEG format
3 * Copyright (c) 2001, 2002, 2003 Fabrice Bellard.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library 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 GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include "avformat.h"
20
21#define Y4M_MAGIC "YUV4MPEG2"
22#define Y4M_FRAME_MAGIC "FRAME"
23#define Y4M_LINE_MAX 256
24
764ef400 25#ifdef CONFIG_ENCODERS
2744ca9a 26
2744ca9a 27static int yuv4_generate_header(AVFormatContext *s, char* buf)
0b9fb89d
FB
28{
29 AVStream *st;
30 int width, height;
5ff85f1d 31 int raten, rated, aspectn, aspectd, n;
2744ca9a 32 char inter;
0b9fb89d 33
0b9fb89d
FB
34 st = s->streams[0];
35 width = st->codec.width;
36 height = st->codec.height;
14bea432 37
14bea432 38 av_reduce(&raten, &rated, st->codec.frame_rate, st->codec.frame_rate_base, (1UL<<31)-1);
2744ca9a 39
5ff85f1d
MN
40 aspectn = st->codec.sample_aspect_ratio.num;
41 aspectd = st->codec.sample_aspect_ratio.den;
0b9fb89d 42
2744ca9a
RS
43 inter = 'p'; /* progressive is the default */
44 if (st->codec.coded_frame && st->codec.coded_frame->interlaced_frame) {
9dad924e 45 inter = st->codec.coded_frame->top_field_first ? 't' : 'b';
2744ca9a 46 }
0b9fb89d
FB
47
48 /* construct stream header, if this is the first frame */
2744ca9a 49 n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
0b9fb89d
FB
50 Y4M_MAGIC,
51 width,
52 height,
53 raten, rated,
2744ca9a
RS
54 inter,
55 aspectn, aspectd,
56 (st->codec.pix_fmt == PIX_FMT_YUV411P) ? " XYSCSS=411" : "");
57
58 return n;
0b9fb89d
FB
59}
60
61static int yuv4_write_packet(AVFormatContext *s, int stream_index,
49057904 62 const uint8_t *buf, int size, int64_t pts)
0b9fb89d
FB
63{
64 AVStream *st = s->streams[stream_index];
65 ByteIOContext *pb = &s->pb;
66 AVPicture *picture;
2744ca9a 67 int* first_pkt = s->priv_data;
0b9fb89d
FB
68 int width, height;
69 int i, m;
2744ca9a 70 char buf2[Y4M_LINE_MAX+1];
0b9fb89d 71 char buf1[20];
0c1a9eda 72 uint8_t *ptr, *ptr1, *ptr2;
0b9fb89d
FB
73
74 picture = (AVPicture *)buf;
75
2744ca9a
RS
76 /* for the first packet we have to output the header as well */
77 if (*first_pkt) {
78 *first_pkt = 0;
79 if (yuv4_generate_header(s, buf2) < 0) {
bc874dae 80 av_log(s, AV_LOG_ERROR, "Error. YUV4MPEG stream header write failed.\n");
2744ca9a
RS
81 return -EIO;
82 } else {
83 put_buffer(pb, buf2, strlen(buf2));
84 }
85 }
86
0b9fb89d 87 /* construct frame header */
2744ca9a 88
a63d4933 89 m = snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
0b9fb89d
FB
90 put_buffer(pb, buf1, strlen(buf1));
91
92 width = st->codec.width;
93 height = st->codec.height;
94
95 ptr = picture->data[0];
96 for(i=0;i<height;i++) {
97 put_buffer(pb, ptr, width);
98 ptr += picture->linesize[0];
99 }
100
101 height >>= 1;
102 width >>= 1;
103 ptr1 = picture->data[1];
104 ptr2 = picture->data[2];
105 for(i=0;i<height;i++) { /* Cb */
106 put_buffer(pb, ptr1, width);
107 ptr1 += picture->linesize[1];
108 }
109 for(i=0;i<height;i++) { /* Cr */
110 put_buffer(pb, ptr2, width);
111 ptr2 += picture->linesize[2];
112 }
113 put_flush_packet(pb);
114 return 0;
115}
116
2744ca9a
RS
117static int yuv4_write_header(AVFormatContext *s)
118{
119 int* first_pkt = s->priv_data;
120
121 if (s->nb_streams != 1)
122 return -EIO;
123
124 if (s->streams[0]->codec.pix_fmt == PIX_FMT_YUV411P) {
bc874dae 125 av_log(s, AV_LOG_ERROR, "Warning: generating non-standard 4:1:1 YUV stream, some mjpegtools might not work.\n");
2744ca9a
RS
126 }
127 else if (s->streams[0]->codec.pix_fmt != PIX_FMT_YUV420P) {
bc874dae 128 av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg only handles 4:2:0, 4:1:1 YUV data. Use -pix_fmt to select one.\n");
2744ca9a
RS
129 return -EIO;
130 }
131
132 *first_pkt = 1;
133 return 0;
134}
135
0b9fb89d
FB
136static int yuv4_write_trailer(AVFormatContext *s)
137{
138 return 0;
139}
140
141AVOutputFormat yuv4mpegpipe_oformat = {
142 "yuv4mpegpipe",
143 "YUV4MPEG pipe format",
144 "",
145 "yuv4mpeg",
2744ca9a 146 sizeof(int),
0b9fb89d
FB
147 CODEC_ID_NONE,
148 CODEC_ID_RAWVIDEO,
149 yuv4_write_header,
150 yuv4_write_packet,
151 yuv4_write_trailer,
152 .flags = AVFMT_RAWPICTURE,
153};
764ef400 154#endif //CONFIG_ENCODERS
0b9fb89d 155
d9a916e2
CY
156/* Header size increased to allow room for optional flags */
157#define MAX_YUV4_HEADER 80
2864dfd5
RFI
158#define MAX_FRAME_HEADER 10
159
160static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap)
161{
162 char header[MAX_YUV4_HEADER+1];
163 int i;
164 ByteIOContext *pb = &s->pb;
165 int width, height, raten, rated, aspectn, aspectd;
166 char lacing;
167 AVStream *st;
168
169 for (i=0; i<MAX_YUV4_HEADER; i++) {
170 header[i] = get_byte(pb);
171 if (header[i] == '\n') {
172 header[i+1] = 0;
173 break;
174 }
175 }
176 if (i == MAX_YUV4_HEADER) return -1;
177 if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) return -1;
178 sscanf(header+strlen(Y4M_MAGIC), " W%d H%d F%d:%d I%c A%d:%d",
179 &width, &height, &raten, &rated, &lacing, &aspectn, &aspectd);
180
181 st = av_new_stream(s, 0);
182 st = s->streams[0];
183 st->codec.width = width;
184 st->codec.height = height;
185 av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1);
186 st->codec.frame_rate = raten;
187 st->codec.frame_rate_base = rated;
188 st->codec.pix_fmt = PIX_FMT_YUV420P;
189 st->codec.codec_type = CODEC_TYPE_VIDEO;
190 st->codec.codec_id = CODEC_ID_RAWVIDEO;
5ff85f1d 191 st->codec.sample_aspect_ratio= (AVRational){aspectn, aspectd};
2864dfd5
RFI
192
193 return 0;
194}
195
196static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
197{
198 int i;
199 char header[MAX_FRAME_HEADER+1];
200 int packet_size, ret, width, height;
201 AVStream *st = s->streams[0];
202
203 for (i=0; i<MAX_FRAME_HEADER; i++) {
204 header[i] = get_byte(&s->pb);
205 if (header[i] == '\n') {
206 header[i+1] = 0;
207 break;
208 }
209 }
210 if (i == MAX_FRAME_HEADER) return -1;
211 if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC))) return -1;
212
213 width = st->codec.width;
214 height = st->codec.height;
215
216 packet_size = avpicture_get_size(st->codec.pix_fmt, width, height);
217 if (packet_size < 0)
218 av_abort();
219
220 if (av_new_packet(pkt, packet_size) < 0)
221 return -EIO;
222
223 pkt->stream_index = 0;
224 ret = get_buffer(&s->pb, pkt->data, pkt->size);
225 if (ret != pkt->size) {
226 av_free_packet(pkt);
227 return -EIO;
228 } else {
229 return 0;
230 }
231}
232
233static int yuv4_read_close(AVFormatContext *s)
234{
235 return 0;
236}
237
238AVInputFormat yuv4mpegpipe_iformat = {
239 "yuv4mpegpipe",
240 "YUV4MPEG pipe format",
241 0,
242 NULL,
243 yuv4_read_header,
244 yuv4_read_packet,
245 yuv4_read_close,
246 .extensions = "yuv4mpeg"
247};
248
249int yuv4mpeg_init(void)
250{
251 av_register_input_format(&yuv4mpegpipe_iformat);
764ef400 252#ifdef CONFIG_ENCODERS
2864dfd5 253 av_register_output_format(&yuv4mpegpipe_oformat);
764ef400 254#endif //CONFIG_ENCODERS
2864dfd5
RFI
255 return 0;
256}
0b9fb89d 257