2 * Copyright (C) 2005 Matthieu CASTET
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavcodec/avcodec.h"
23 #include "libavcodec/bytestream.h"
24 #include "libavcodec/flac.h"
29 #define OGG_FLAC_METADATA_TYPE_STREAMINFO 0x7F
30 #define OGG_FLAC_MAGIC "\177FLAC"
31 #define OGG_FLAC_MAGIC_SIZE sizeof(OGG_FLAC_MAGIC)-1
34 flac_header (AVFormatContext
* s
, int idx
)
36 struct ogg
*ogg
= s
->priv_data
;
37 struct ogg_stream
*os
= ogg
->streams
+ idx
;
38 AVStream
*st
= s
->streams
[idx
];
42 if (os
->buf
[os
->pstart
] == 0xff)
45 bytestream2_init(&gb
, os
->buf
+ os
->pstart
, os
->psize
);
46 mdt
= bytestream2_get_byte(&gb
) & 0x7F;
48 if (mdt
== OGG_FLAC_METADATA_TYPE_STREAMINFO
) {
51 if (bytestream2_get_bytes_left(&gb
) < 4 + 4 + 4 + 4 + FLAC_STREAMINFO_SIZE
)
52 return AVERROR_INVALIDDATA
;
53 bytestream2_skipu(&gb
, 4); /* "FLAC" */
54 if (bytestream2_get_byteu(&gb
) != 1) /* unsupported major version */
56 bytestream2_skipu(&gb
, 1 + 2); /* minor version + header count */
57 bytestream2_skipu(&gb
, 4); /* "fLaC" */
59 /* METADATA_BLOCK_HEADER */
60 if (bytestream2_get_be32u(&gb
) != FLAC_STREAMINFO_SIZE
)
63 st
->codecpar
->codec_type
= AVMEDIA_TYPE_AUDIO
;
64 st
->codecpar
->codec_id
= AV_CODEC_ID_FLAC
;
65 ffstream(st
)->need_parsing
= AVSTREAM_PARSE_HEADERS
;
67 if ((ret
= ff_alloc_extradata(st
->codecpar
, FLAC_STREAMINFO_SIZE
)) < 0)
69 bytestream2_get_bufferu(&gb
, st
->codecpar
->extradata
, FLAC_STREAMINFO_SIZE
);
71 samplerate
= AV_RB24(st
->codecpar
->extradata
+ 10) >> 4;
73 return AVERROR_INVALIDDATA
;
75 avpriv_set_pts_info(st
, 64, 1, samplerate
);
76 } else if (mdt
== FLAC_METADATA_TYPE_VORBIS_COMMENT
) {
77 ff_vorbis_stream_comment(s
, st
, os
->buf
+ os
->pstart
+ 4, os
->psize
- 4);
84 flac_packet (AVFormatContext
* s
, int idx
)
86 struct ogg
*ogg
= s
->priv_data
;
87 struct ogg_stream
*os
= ogg
->streams
+ idx
;
88 AVStream
*st
= s
->streams
[idx
];
91 if (os
->psize
> OGG_FLAC_MAGIC_SIZE
&&
99 ((os
->buf
[os
->pstart
] & 0x7F) == FLAC_METADATA_TYPE_VORBIS_COMMENT
)) {
100 ret
= ff_vorbis_update_metadata(s
, st
, os
->buf
+ os
->pstart
+ 4,
112 old_flac_header (AVFormatContext
* s
, int idx
)
114 struct ogg
*ogg
= s
->priv_data
;
115 AVStream
*st
= s
->streams
[idx
];
116 struct ogg_stream
*os
= ogg
->streams
+ idx
;
117 AVCodecParserContext
*parser
= av_parser_init(AV_CODEC_ID_FLAC
);
118 AVCodecContext
*avctx
;
125 st
->codecpar
->codec_type
= AVMEDIA_TYPE_AUDIO
;
126 st
->codecpar
->codec_id
= AV_CODEC_ID_FLAC
;
128 avctx
= avcodec_alloc_context3(NULL
);
130 ret
= AVERROR(ENOMEM
);
134 ret
= avcodec_parameters_to_context(avctx
, st
->codecpar
);
138 parser
->flags
= PARSER_FLAG_COMPLETE_FRAMES
;
139 av_parser_parse2(parser
, avctx
,
140 &data
, &size
, os
->buf
+ os
->pstart
, os
->psize
,
141 AV_NOPTS_VALUE
, AV_NOPTS_VALUE
, -1);
143 av_parser_close(parser
);
145 if (avctx
->sample_rate
) {
146 avpriv_set_pts_info(st
, 64, 1, avctx
->sample_rate
);
147 avcodec_free_context(&avctx
);
151 avcodec_free_context(&avctx
);
154 av_parser_close(parser
);
155 avcodec_free_context(&avctx
);
159 const struct ogg_codec ff_flac_codec
= {
160 .magic
= OGG_FLAC_MAGIC
,
161 .magicsize
= OGG_FLAC_MAGIC_SIZE
,
162 .header
= flac_header
,
164 .packet
= flac_packet
,
167 const struct ogg_codec ff_old_flac_codec
= {
170 .header
= old_flac_header
,