avformat/http: return EINVAL if ff_http_do_new_request is called with non-http URLContext
[ffmpeg.git] / libavformat / rawenc.c
1 /*
2  * RAW muxers
3  * Copyright (c) 2001 Fabrice Bellard
4  * Copyright (c) 2005 Alex Beregszaszi
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include "libavutil/intreadwrite.h"
24
25 #include "avformat.h"
26 #include "rawenc.h"
27 #include "internal.h"
28
29 int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
30 {
31     avio_write(s->pb, pkt->data, pkt->size);
32     return 0;
33 }
34
35 static int force_one_stream(AVFormatContext *s)
36 {
37     if (s->nb_streams != 1) {
38         av_log(s, AV_LOG_ERROR, "%s files have exactly one stream\n",
39                s->oformat->name);
40         return AVERROR(EINVAL);
41     }
42     return 0;
43 }
44
45 /* Note: Do not forget to add new entries to the Makefile as well. */
46
47 #if CONFIG_AC3_MUXER
48 AVOutputFormat ff_ac3_muxer = {
49     .name              = "ac3",
50     .long_name         = NULL_IF_CONFIG_SMALL("raw AC-3"),
51     .mime_type         = "audio/x-ac3",
52     .extensions        = "ac3",
53     .audio_codec       = AV_CODEC_ID_AC3,
54     .video_codec       = AV_CODEC_ID_NONE,
55     .write_header      = force_one_stream,
56     .write_packet      = ff_raw_write_packet,
57     .flags             = AVFMT_NOTIMESTAMPS,
58 };
59 #endif
60
61 #if CONFIG_ADX_MUXER
62
63 static int adx_write_trailer(AVFormatContext *s)
64 {
65     AVIOContext *pb = s->pb;
66     AVCodecParameters *par = s->streams[0]->codecpar;
67
68     if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
69         int64_t file_size = avio_tell(pb);
70         uint64_t sample_count = (file_size - 36) / par->channels / 18 * 32;
71         if (sample_count <= UINT32_MAX) {
72             avio_seek(pb, 12, SEEK_SET);
73             avio_wb32(pb, sample_count);
74             avio_seek(pb, file_size, SEEK_SET);
75         }
76     }
77
78     return 0;
79 }
80
81 AVOutputFormat ff_adx_muxer = {
82     .name              = "adx",
83     .long_name         = NULL_IF_CONFIG_SMALL("CRI ADX"),
84     .extensions        = "adx",
85     .audio_codec       = AV_CODEC_ID_ADPCM_ADX,
86     .video_codec       = AV_CODEC_ID_NONE,
87     .write_header      = force_one_stream,
88     .write_packet      = ff_raw_write_packet,
89     .write_trailer     = adx_write_trailer,
90     .flags             = AVFMT_NOTIMESTAMPS,
91 };
92 #endif
93
94 #if CONFIG_APTX_MUXER
95 AVOutputFormat ff_aptx_muxer = {
96     .name              = "aptx",
97     .long_name         = NULL_IF_CONFIG_SMALL("raw aptX (Audio Processing Technology for Bluetooth)"),
98     .extensions        = "aptx",
99     .audio_codec       = AV_CODEC_ID_APTX,
100     .video_codec       = AV_CODEC_ID_NONE,
101     .write_header      = force_one_stream,
102     .write_packet      = ff_raw_write_packet,
103     .flags             = AVFMT_NOTIMESTAMPS,
104 };
105 #endif
106
107 #if CONFIG_CAVSVIDEO_MUXER
108 AVOutputFormat ff_cavsvideo_muxer = {
109     .name              = "cavsvideo",
110     .long_name         = NULL_IF_CONFIG_SMALL("raw Chinese AVS (Audio Video Standard) video"),
111     .extensions        = "cavs",
112     .audio_codec       = AV_CODEC_ID_NONE,
113     .video_codec       = AV_CODEC_ID_CAVS,
114     .write_header      = force_one_stream,
115     .write_packet      = ff_raw_write_packet,
116     .flags             = AVFMT_NOTIMESTAMPS,
117 };
118 #endif
119
120 #if CONFIG_DATA_MUXER
121 AVOutputFormat ff_data_muxer = {
122     .name              = "data",
123     .long_name         = NULL_IF_CONFIG_SMALL("raw data"),
124     .write_header      = force_one_stream,
125     .write_packet      = ff_raw_write_packet,
126     .flags             = AVFMT_NOTIMESTAMPS,
127 };
128 #endif
129
130 #if CONFIG_DIRAC_MUXER
131 AVOutputFormat ff_dirac_muxer = {
132     .name              = "dirac",
133     .long_name         = NULL_IF_CONFIG_SMALL("raw Dirac"),
134     .extensions        = "drc,vc2",
135     .audio_codec       = AV_CODEC_ID_NONE,
136     .video_codec       = AV_CODEC_ID_DIRAC,
137     .write_header      = force_one_stream,
138     .write_packet      = ff_raw_write_packet,
139     .flags             = AVFMT_NOTIMESTAMPS,
140 };
141 #endif
142
143 #if CONFIG_DNXHD_MUXER
144 AVOutputFormat ff_dnxhd_muxer = {
145     .name              = "dnxhd",
146     .long_name         = NULL_IF_CONFIG_SMALL("raw DNxHD (SMPTE VC-3)"),
147     .extensions        = "dnxhd,dnxhr",
148     .audio_codec       = AV_CODEC_ID_NONE,
149     .video_codec       = AV_CODEC_ID_DNXHD,
150     .write_header      = force_one_stream,
151     .write_packet      = ff_raw_write_packet,
152     .flags             = AVFMT_NOTIMESTAMPS,
153 };
154 #endif
155
156 #if CONFIG_DTS_MUXER
157 AVOutputFormat ff_dts_muxer = {
158     .name              = "dts",
159     .long_name         = NULL_IF_CONFIG_SMALL("raw DTS"),
160     .mime_type         = "audio/x-dca",
161     .extensions        = "dts",
162     .audio_codec       = AV_CODEC_ID_DTS,
163     .video_codec       = AV_CODEC_ID_NONE,
164     .write_header      = force_one_stream,
165     .write_packet      = ff_raw_write_packet,
166     .flags             = AVFMT_NOTIMESTAMPS,
167 };
168 #endif
169
170 #if CONFIG_EAC3_MUXER
171 AVOutputFormat ff_eac3_muxer = {
172     .name              = "eac3",
173     .long_name         = NULL_IF_CONFIG_SMALL("raw E-AC-3"),
174     .mime_type         = "audio/x-eac3",
175     .extensions        = "eac3",
176     .audio_codec       = AV_CODEC_ID_EAC3,
177     .video_codec       = AV_CODEC_ID_NONE,
178     .write_header      = force_one_stream,
179     .write_packet      = ff_raw_write_packet,
180     .flags             = AVFMT_NOTIMESTAMPS,
181 };
182 #endif
183
184 #if CONFIG_G722_MUXER
185 AVOutputFormat ff_g722_muxer = {
186     .name              = "g722",
187     .long_name         = NULL_IF_CONFIG_SMALL("raw G.722"),
188     .mime_type         = "audio/G722",
189     .extensions        = "g722",
190     .audio_codec       = AV_CODEC_ID_ADPCM_G722,
191     .video_codec       = AV_CODEC_ID_NONE,
192     .write_header      = force_one_stream,
193     .write_packet      = ff_raw_write_packet,
194     .flags             = AVFMT_NOTIMESTAMPS,
195 };
196 #endif
197
198 #if CONFIG_G723_1_MUXER
199 AVOutputFormat ff_g723_1_muxer = {
200     .name              = "g723_1",
201     .long_name         = NULL_IF_CONFIG_SMALL("raw G.723.1"),
202     .mime_type         = "audio/g723",
203     .extensions        = "tco,rco",
204     .audio_codec       = AV_CODEC_ID_G723_1,
205     .video_codec       = AV_CODEC_ID_NONE,
206     .write_header      = force_one_stream,
207     .write_packet      = ff_raw_write_packet,
208     .flags             = AVFMT_NOTIMESTAMPS,
209 };
210 #endif
211
212 #if CONFIG_G726_MUXER
213 AVOutputFormat ff_g726_muxer = {
214     .name              = "g726",
215     .long_name         = NULL_IF_CONFIG_SMALL("raw big-endian G.726 (\"left-justified\")"),
216     .audio_codec       = AV_CODEC_ID_ADPCM_G726,
217     .video_codec       = AV_CODEC_ID_NONE,
218     .write_header      = force_one_stream,
219     .write_packet      = ff_raw_write_packet,
220     .flags             = AVFMT_NOTIMESTAMPS,
221 };
222 #endif
223
224 #if CONFIG_G726LE_MUXER
225 AVOutputFormat ff_g726le_muxer = {
226     .name              = "g726le",
227     .long_name         = NULL_IF_CONFIG_SMALL("raw little-endian G.726 (\"right-justified\")"),
228     .audio_codec       = AV_CODEC_ID_ADPCM_G726LE,
229     .video_codec       = AV_CODEC_ID_NONE,
230     .write_header      = force_one_stream,
231     .write_packet      = ff_raw_write_packet,
232     .flags             = AVFMT_NOTIMESTAMPS,
233 };
234 #endif
235
236 #if CONFIG_GSM_MUXER
237 AVOutputFormat ff_gsm_muxer = {
238     .name              = "gsm",
239     .long_name         = NULL_IF_CONFIG_SMALL("raw GSM"),
240     .mime_type         = "audio/x-gsm",
241     .extensions        = "gsm",
242     .audio_codec       = AV_CODEC_ID_GSM,
243     .video_codec       = AV_CODEC_ID_NONE,
244     .write_header      = force_one_stream,
245     .write_packet      = ff_raw_write_packet,
246     .flags             = AVFMT_NOTIMESTAMPS,
247 };
248 #endif
249
250 #if CONFIG_H261_MUXER
251 AVOutputFormat ff_h261_muxer = {
252     .name              = "h261",
253     .long_name         = NULL_IF_CONFIG_SMALL("raw H.261"),
254     .mime_type         = "video/x-h261",
255     .extensions        = "h261",
256     .audio_codec       = AV_CODEC_ID_NONE,
257     .video_codec       = AV_CODEC_ID_H261,
258     .write_header      = force_one_stream,
259     .write_packet      = ff_raw_write_packet,
260     .flags             = AVFMT_NOTIMESTAMPS,
261 };
262 #endif
263
264 #if CONFIG_H263_MUXER
265 AVOutputFormat ff_h263_muxer = {
266     .name              = "h263",
267     .long_name         = NULL_IF_CONFIG_SMALL("raw H.263"),
268     .mime_type         = "video/x-h263",
269     .extensions        = "h263",
270     .audio_codec       = AV_CODEC_ID_NONE,
271     .video_codec       = AV_CODEC_ID_H263,
272     .write_header      = force_one_stream,
273     .write_packet      = ff_raw_write_packet,
274     .flags             = AVFMT_NOTIMESTAMPS,
275 };
276 #endif
277
278 #if CONFIG_H264_MUXER
279 static int h264_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
280 {
281     AVStream *st = s->streams[0];
282     if (pkt->size >= 5 && AV_RB32(pkt->data) != 0x0000001 &&
283                           AV_RB24(pkt->data) != 0x000001)
284         return ff_stream_add_bitstream_filter(st, "h264_mp4toannexb", NULL);
285     return 1;
286 }
287
288 AVOutputFormat ff_h264_muxer = {
289     .name              = "h264",
290     .long_name         = NULL_IF_CONFIG_SMALL("raw H.264 video"),
291     .extensions        = "h264,264",
292     .audio_codec       = AV_CODEC_ID_NONE,
293     .video_codec       = AV_CODEC_ID_H264,
294     .write_header      = force_one_stream,
295     .write_packet      = ff_raw_write_packet,
296     .check_bitstream   = h264_check_bitstream,
297     .flags             = AVFMT_NOTIMESTAMPS,
298 };
299 #endif
300
301 #if CONFIG_HEVC_MUXER
302 static int hevc_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
303 {
304     AVStream *st = s->streams[0];
305     if (pkt->size >= 5 && AV_RB32(pkt->data) != 0x0000001 &&
306                           AV_RB24(pkt->data) != 0x000001)
307         return ff_stream_add_bitstream_filter(st, "hevc_mp4toannexb", NULL);
308     return 1;
309 }
310
311 AVOutputFormat ff_hevc_muxer = {
312     .name              = "hevc",
313     .long_name         = NULL_IF_CONFIG_SMALL("raw HEVC video"),
314     .extensions        = "hevc,h265,265",
315     .audio_codec       = AV_CODEC_ID_NONE,
316     .video_codec       = AV_CODEC_ID_HEVC,
317     .write_header      = force_one_stream,
318     .write_packet      = ff_raw_write_packet,
319     .check_bitstream   = hevc_check_bitstream,
320     .flags             = AVFMT_NOTIMESTAMPS,
321 };
322 #endif
323
324 #if CONFIG_M4V_MUXER
325 AVOutputFormat ff_m4v_muxer = {
326     .name              = "m4v",
327     .long_name         = NULL_IF_CONFIG_SMALL("raw MPEG-4 video"),
328     .extensions        = "m4v",
329     .audio_codec       = AV_CODEC_ID_NONE,
330     .video_codec       = AV_CODEC_ID_MPEG4,
331     .write_header      = force_one_stream,
332     .write_packet      = ff_raw_write_packet,
333     .flags             = AVFMT_NOTIMESTAMPS,
334 };
335 #endif
336
337 #if CONFIG_MJPEG_MUXER
338 AVOutputFormat ff_mjpeg_muxer = {
339     .name              = "mjpeg",
340     .long_name         = NULL_IF_CONFIG_SMALL("raw MJPEG video"),
341     .mime_type         = "video/x-mjpeg",
342     .extensions        = "mjpg,mjpeg",
343     .audio_codec       = AV_CODEC_ID_NONE,
344     .video_codec       = AV_CODEC_ID_MJPEG,
345     .write_header      = force_one_stream,
346     .write_packet      = ff_raw_write_packet,
347     .flags             = AVFMT_NOTIMESTAMPS,
348 };
349 #endif
350
351 #if CONFIG_SINGLEJPEG_MUXER
352 AVOutputFormat ff_singlejpeg_muxer = {
353     .name              = "singlejpeg",
354     .long_name         = NULL_IF_CONFIG_SMALL("JPEG single image"),
355     .mime_type         = "image/jpeg",
356     .audio_codec       = AV_CODEC_ID_NONE,
357     .video_codec       = AV_CODEC_ID_MJPEG,
358     .write_packet      = ff_raw_write_packet,
359     .flags             = AVFMT_NOTIMESTAMPS,
360     .write_header      = force_one_stream,
361 };
362 #endif
363
364 #if CONFIG_MLP_MUXER
365 AVOutputFormat ff_mlp_muxer = {
366     .name              = "mlp",
367     .long_name         = NULL_IF_CONFIG_SMALL("raw MLP"),
368     .extensions        = "mlp",
369     .audio_codec       = AV_CODEC_ID_MLP,
370     .video_codec       = AV_CODEC_ID_NONE,
371     .write_header      = force_one_stream,
372     .write_packet      = ff_raw_write_packet,
373     .flags             = AVFMT_NOTIMESTAMPS,
374 };
375 #endif
376
377 #if CONFIG_MP2_MUXER
378 AVOutputFormat ff_mp2_muxer = {
379     .name              = "mp2",
380     .long_name         = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
381     .mime_type         = "audio/mpeg",
382     .extensions        = "mp2,m2a,mpa",
383     .audio_codec       = AV_CODEC_ID_MP2,
384     .video_codec       = AV_CODEC_ID_NONE,
385     .write_packet      = ff_raw_write_packet,
386     .flags             = AVFMT_NOTIMESTAMPS,
387 };
388 #endif
389
390 #if CONFIG_MPEG1VIDEO_MUXER
391 AVOutputFormat ff_mpeg1video_muxer = {
392     .name              = "mpeg1video",
393     .long_name         = NULL_IF_CONFIG_SMALL("raw MPEG-1 video"),
394     .mime_type         = "video/mpeg",
395     .extensions        = "mpg,mpeg,m1v",
396     .audio_codec       = AV_CODEC_ID_NONE,
397     .video_codec       = AV_CODEC_ID_MPEG1VIDEO,
398     .write_header      = force_one_stream,
399     .write_packet      = ff_raw_write_packet,
400     .flags             = AVFMT_NOTIMESTAMPS,
401 };
402 #endif
403
404 #if CONFIG_MPEG2VIDEO_MUXER
405 AVOutputFormat ff_mpeg2video_muxer = {
406     .name              = "mpeg2video",
407     .long_name         = NULL_IF_CONFIG_SMALL("raw MPEG-2 video"),
408     .extensions        = "m2v",
409     .audio_codec       = AV_CODEC_ID_NONE,
410     .video_codec       = AV_CODEC_ID_MPEG2VIDEO,
411     .write_header      = force_one_stream,
412     .write_packet      = ff_raw_write_packet,
413     .flags             = AVFMT_NOTIMESTAMPS,
414 };
415 #endif
416
417 #if CONFIG_RAWVIDEO_MUXER
418 AVOutputFormat ff_rawvideo_muxer = {
419     .name              = "rawvideo",
420     .long_name         = NULL_IF_CONFIG_SMALL("raw video"),
421     .extensions        = "yuv,rgb",
422     .audio_codec       = AV_CODEC_ID_NONE,
423     .video_codec       = AV_CODEC_ID_RAWVIDEO,
424     .write_packet      = ff_raw_write_packet,
425     .flags             = AVFMT_NOTIMESTAMPS,
426 };
427 #endif
428
429 #if CONFIG_TRUEHD_MUXER
430 AVOutputFormat ff_truehd_muxer = {
431     .name              = "truehd",
432     .long_name         = NULL_IF_CONFIG_SMALL("raw TrueHD"),
433     .extensions        = "thd",
434     .audio_codec       = AV_CODEC_ID_TRUEHD,
435     .video_codec       = AV_CODEC_ID_NONE,
436     .write_header      = force_one_stream,
437     .write_packet      = ff_raw_write_packet,
438     .flags             = AVFMT_NOTIMESTAMPS,
439 };
440 #endif
441
442 #if CONFIG_VC1_MUXER
443 AVOutputFormat ff_vc1_muxer = {
444     .name              = "vc1",
445     .long_name         = NULL_IF_CONFIG_SMALL("raw VC-1 video"),
446     .extensions        = "vc1",
447     .audio_codec       = AV_CODEC_ID_NONE,
448     .video_codec       = AV_CODEC_ID_VC1,
449     .write_header      = force_one_stream,
450     .write_packet      = ff_raw_write_packet,
451     .flags             = AVFMT_NOTIMESTAMPS,
452 };
453 #endif