comparison yuv4mpeg.c @ 18:01e35bb2689a libavformat

extracted yuv4mpeg from img.c (untested)
author bellard
date Sat, 11 Jan 2003 04:58:36 +0000
parents
children a58a8a53eb46
comparison
equal deleted inserted replaced
17:e1200dd82537 18:01e35bb2689a
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
25 static int yuv4_write_header(AVFormatContext *s)
26 {
27 AVStream *st;
28 int width, height;
29 int raten, rated, aspectn, aspectd, fps, fps1, n;
30 char buf[Y4M_LINE_MAX+1];
31
32 if (s->nb_streams != 1)
33 return -EIO;
34
35 st = s->streams[0];
36 width = st->codec.width;
37 height = st->codec.height;
38
39 fps = st->codec.frame_rate;
40 fps1 = (((float)fps / FRAME_RATE_BASE) * 1000);
41
42 /* Sorry about this messy code, but mpeg2enc is very picky about
43 * the framerates it accepts. */
44 switch(fps1) {
45 case 23976:
46 raten = 24000; /* turn the framerate into a ratio */
47 rated = 1001;
48 break;
49 case 29970:
50 raten = 30000;
51 rated = 1001;
52 break;
53 case 25000:
54 raten = 25;
55 rated = 1;
56 break;
57 case 30000:
58 raten = 30;
59 rated = 1;
60 break;
61 case 24000:
62 raten = 24;
63 rated = 1;
64 break;
65 case 50000:
66 raten = 50;
67 rated = 1;
68 break;
69 case 59940:
70 raten = 60000;
71 rated = 1001;
72 break;
73 case 60000:
74 raten = 60;
75 rated = 1;
76 break;
77 default:
78 raten = fps1; /* this setting should work, but often doesn't */
79 rated = 1000;
80 break;
81 }
82
83 aspectn = 1;
84 aspectd = 1; /* ffmpeg always uses a 1:1 aspect ratio */
85
86 /* construct stream header, if this is the first frame */
87 n = snprintf(buf, sizeof(buf), "%s W%d H%d F%d:%d I%s A%d:%d\n",
88 Y4M_MAGIC,
89 width,
90 height,
91 raten, rated,
92 "p", /* ffmpeg seems to only output progressive video */
93 aspectn, aspectd);
94 if (n < 0) {
95 fprintf(stderr, "Error. YUV4MPEG stream header write failed.\n");
96 return -EIO;
97 } else {
98 put_buffer(&s->pb, buf, strlen(buf));
99 }
100 return 0;
101 }
102
103 static int yuv4_write_packet(AVFormatContext *s, int stream_index,
104 UINT8 *buf, int size, int force_pts)
105 {
106 AVStream *st = s->streams[stream_index];
107 ByteIOContext *pb = &s->pb;
108 AVPicture *picture;
109 int width, height;
110 int i, m;
111 char buf1[20];
112 UINT8 *ptr, *ptr1, *ptr2;
113
114 picture = (AVPicture *)buf;
115
116 /* construct frame header */
117 m = snprintf(buf1, sizeof(buf1), "%s \n", Y4M_FRAME_MAGIC);
118 put_buffer(pb, buf1, strlen(buf1));
119
120 width = st->codec.width;
121 height = st->codec.height;
122
123 ptr = picture->data[0];
124 for(i=0;i<height;i++) {
125 put_buffer(pb, ptr, width);
126 ptr += picture->linesize[0];
127 }
128
129 height >>= 1;
130 width >>= 1;
131 ptr1 = picture->data[1];
132 ptr2 = picture->data[2];
133 for(i=0;i<height;i++) { /* Cb */
134 put_buffer(pb, ptr1, width);
135 ptr1 += picture->linesize[1];
136 }
137 for(i=0;i<height;i++) { /* Cr */
138 put_buffer(pb, ptr2, width);
139 ptr2 += picture->linesize[2];
140 }
141 put_flush_packet(pb);
142 return 0;
143 }
144
145 static int yuv4_write_trailer(AVFormatContext *s)
146 {
147 return 0;
148 }
149
150 AVOutputFormat yuv4mpegpipe_oformat = {
151 "yuv4mpegpipe",
152 "YUV4MPEG pipe format",
153 "",
154 "yuv4mpeg",
155 0,
156 CODEC_ID_NONE,
157 CODEC_ID_RAWVIDEO,
158 yuv4_write_header,
159 yuv4_write_packet,
160 yuv4_write_trailer,
161 .flags = AVFMT_RAWPICTURE,
162 };
163
164