annotate yuv4mpeg.c @ 83:5e7f9b16e0a8 libavformat

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