annotate yuv4mpeg.c @ 153:41d4f3a86c98 libavformat

cleanup; BSoundRecorder moved to Experimental namespace
author mmu_man
date Sun, 15 Jun 2003 17:06:28 +0000
parents 25062c9b1f86
children d1290621cc6a
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;
85
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 65
diff changeset
29 int raten, rated, aspectn, aspectd, fps, fps1, n, gcd;
18
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;
85
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 65
diff changeset
38
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 65
diff changeset
39 #if 1
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 65
diff changeset
40 //this is identical to the code below for exact fps
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 65
diff changeset
41 av_reduce(&raten, &rated, st->codec.frame_rate, st->codec.frame_rate_base, (1UL<<31)-1);
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 65
diff changeset
42 #else
18
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
43 fps = st->codec.frame_rate;
85
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 65
diff changeset
44 fps1 = (((float)fps / st->codec.frame_rate_base) * 1000);
18
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
45
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
46 /* Sorry about this messy code, but mpeg2enc is very picky about
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
47 * the framerates it accepts. */
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
48 switch(fps1) {
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
49 case 23976:
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
50 raten = 24000; /* turn the framerate into a ratio */
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 29970:
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
54 raten = 30000;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
55 rated = 1001;
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 25000:
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
58 raten = 25;
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 30000:
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
62 raten = 30;
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 24000:
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
66 raten = 24;
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 50000:
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
70 raten = 50;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
71 rated = 1;
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 59940:
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
74 raten = 60000;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
75 rated = 1001;
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 case 60000:
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
78 raten = 60;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
79 rated = 1;
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 default:
85
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 65
diff changeset
82 raten = st->codec.frame_rate; /* this setting should work, but often doesn't */
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 65
diff changeset
83 rated = st->codec.frame_rate_base;
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 65
diff changeset
84 gcd= av_gcd(raten, rated);
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 65
diff changeset
85 raten /= gcd;
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 65
diff changeset
86 rated /= gcd;
18
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
87 break;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
88 }
85
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 65
diff changeset
89 #endif
18
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
90
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
91 aspectn = 1;
85
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 65
diff changeset
92 aspectd = 1; /* ffmpeg always uses a 1:1 aspect ratio */ //FIXME not true anymore
18
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
93
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
94 /* construct stream header, if this is the first frame */
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
95 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
96 Y4M_MAGIC,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
97 width,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
98 height,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
99 raten, rated,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
100 "p", /* ffmpeg seems to only output progressive video */
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
101 aspectn, aspectd);
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
102 if (n < 0) {
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
103 fprintf(stderr, "Error. YUV4MPEG stream header write failed.\n");
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
104 return -EIO;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
105 } else {
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
106 put_buffer(&s->pb, buf, strlen(buf));
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
107 }
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
108 return 0;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
109 }
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
110
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
111 static int yuv4_write_packet(AVFormatContext *s, int stream_index,
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 18
diff changeset
112 uint8_t *buf, int size, int force_pts)
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 AVStream *st = s->streams[stream_index];
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
115 ByteIOContext *pb = &s->pb;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
116 AVPicture *picture;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
117 int width, height;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
118 int i, m;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
119 char buf1[20];
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 18
diff changeset
120 uint8_t *ptr, *ptr1, *ptr2;
18
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
121
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
122 picture = (AVPicture *)buf;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
123
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
124 /* construct frame header */
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
125 m = snprintf(buf1, sizeof(buf1), "%s \n", Y4M_FRAME_MAGIC);
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
126 put_buffer(pb, buf1, strlen(buf1));
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
127
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
128 width = st->codec.width;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
129 height = st->codec.height;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
130
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
131 ptr = picture->data[0];
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
132 for(i=0;i<height;i++) {
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
133 put_buffer(pb, ptr, width);
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
134 ptr += picture->linesize[0];
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
135 }
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
136
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
137 height >>= 1;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
138 width >>= 1;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
139 ptr1 = picture->data[1];
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
140 ptr2 = picture->data[2];
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
141 for(i=0;i<height;i++) { /* Cb */
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
142 put_buffer(pb, ptr1, width);
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
143 ptr1 += picture->linesize[1];
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
144 }
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
145 for(i=0;i<height;i++) { /* Cr */
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
146 put_buffer(pb, ptr2, width);
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
147 ptr2 += picture->linesize[2];
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
148 }
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
149 put_flush_packet(pb);
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
150 return 0;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
151 }
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
152
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
153 static int yuv4_write_trailer(AVFormatContext *s)
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
154 {
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
155 return 0;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
156 }
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
157
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
158 AVOutputFormat yuv4mpegpipe_oformat = {
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
159 "yuv4mpegpipe",
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
160 "YUV4MPEG pipe format",
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
161 "",
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
162 "yuv4mpeg",
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
163 0,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
164 CODEC_ID_NONE,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
165 CODEC_ID_RAWVIDEO,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
166 yuv4_write_header,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
167 yuv4_write_packet,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
168 yuv4_write_trailer,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
169 .flags = AVFMT_RAWPICTURE,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
170 };
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
171
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
172