]> git.sesse.net Git - ffmpeg/blob - libavformat/yuv4mpeg.c
use custom packet allocation only for DV
[ffmpeg] / libavformat / yuv4mpeg.c
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, gcd;
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 #if 1
40     //this is identical to the code below for exact fps
41     av_reduce(&raten, &rated, st->codec.frame_rate, st->codec.frame_rate_base, (1UL<<31)-1);
42 #else
43     fps = st->codec.frame_rate;
44     fps1 = (((float)fps / st->codec.frame_rate_base) * 1000);
45    
46    /* Sorry about this messy code, but mpeg2enc is very picky about
47     * the framerates it accepts. */
48     switch(fps1) {
49     case 23976:
50         raten = 24000; /* turn the framerate into a ratio */
51         rated = 1001;
52         break;
53     case 29970:
54         raten = 30000;
55         rated = 1001;
56         break;
57     case 25000:
58         raten = 25;
59         rated = 1;
60         break;
61     case 30000:
62         raten = 30;
63         rated = 1;
64         break;
65     case 24000:
66         raten = 24;
67         rated = 1;
68         break;
69     case 50000:
70         raten = 50;
71         rated = 1;
72         break;
73     case 59940:
74         raten = 60000;
75         rated = 1001;
76         break;
77     case 60000:
78         raten = 60;
79         rated = 1;
80         break;
81     default:
82         raten = st->codec.frame_rate; /* this setting should work, but often doesn't */
83         rated = st->codec.frame_rate_base;
84         gcd= av_gcd(raten, rated);
85         raten /= gcd;
86         rated /= gcd;
87         break;
88     }
89 #endif
90     
91     aspectn = 1;
92     aspectd = 1;        /* ffmpeg always uses a 1:1 aspect ratio */ //FIXME not true anymore
93
94     /* construct stream header, if this is the first frame */
95     n = snprintf(buf, sizeof(buf), "%s W%d H%d F%d:%d I%s A%d:%d\n",
96                  Y4M_MAGIC,
97                  width,
98                  height,
99                  raten, rated,
100                  "p",                   /* ffmpeg seems to only output progressive video */
101                  aspectn, aspectd);
102     if (n < 0) {
103         fprintf(stderr, "Error. YUV4MPEG stream header write failed.\n");
104         return -EIO;
105     } else {
106         put_buffer(&s->pb, buf, strlen(buf));
107     }
108     return 0;
109 }
110
111 static int yuv4_write_packet(AVFormatContext *s, int stream_index,
112                              uint8_t *buf, int size, int force_pts)
113 {
114     AVStream *st = s->streams[stream_index];
115     ByteIOContext *pb = &s->pb;
116     AVPicture *picture;
117     int width, height;
118     int i, m;
119     char buf1[20];
120     uint8_t *ptr, *ptr1, *ptr2;
121
122     picture = (AVPicture *)buf;
123
124     /* construct frame header */
125     m = snprintf(buf1, sizeof(buf1), "%s \n", Y4M_FRAME_MAGIC);
126     put_buffer(pb, buf1, strlen(buf1));
127
128     width = st->codec.width;
129     height = st->codec.height;
130     
131     ptr = picture->data[0];
132     for(i=0;i<height;i++) {
133         put_buffer(pb, ptr, width);
134         ptr += picture->linesize[0];
135     }
136
137     height >>= 1;
138     width >>= 1;
139     ptr1 = picture->data[1];
140     ptr2 = picture->data[2];
141     for(i=0;i<height;i++) {             /* Cb */
142         put_buffer(pb, ptr1, width);
143         ptr1 += picture->linesize[1];
144     }
145     for(i=0;i<height;i++) {     /* Cr */
146         put_buffer(pb, ptr2, width);
147             ptr2 += picture->linesize[2];
148     }
149     put_flush_packet(pb);
150     return 0;
151 }
152
153 static int yuv4_write_trailer(AVFormatContext *s)
154 {
155     return 0;
156 }
157
158 AVOutputFormat yuv4mpegpipe_oformat = {
159     "yuv4mpegpipe",
160     "YUV4MPEG pipe format",
161     "",
162     "yuv4mpeg",
163     0,
164     CODEC_ID_NONE,
165     CODEC_ID_RAWVIDEO,
166     yuv4_write_header,
167     yuv4_write_packet,
168     yuv4_write_trailer,
169     .flags = AVFMT_RAWPICTURE,
170 };
171
172