1b2859abeSMarcus Overhagen /*
24da4784aSMarcus Overhagen * Copyright (c) 2004-2010 Marcus Overhagen <marcus@overhagen.de>
3b2859abeSMarcus Overhagen *
4b2859abeSMarcus Overhagen * Permission is hereby granted, free of charge, to any person
5b2859abeSMarcus Overhagen * obtaining a copy of this software and associated documentation
6b2859abeSMarcus Overhagen * files (the "Software"), to deal in the Software without restriction,
7b2859abeSMarcus Overhagen * including without limitation the rights to use, copy, modify,
8b2859abeSMarcus Overhagen * merge, publish, distribute, sublicense, and/or sell copies of
9b2859abeSMarcus Overhagen * the Software, and to permit persons to whom the Software is
10b2859abeSMarcus Overhagen * furnished to do so, subject to the following conditions:
11b2859abeSMarcus Overhagen *
12b2859abeSMarcus Overhagen * The above copyright notice and this permission notice shall be
13b2859abeSMarcus Overhagen * included in all copies or substantial portions of the Software.
14b2859abeSMarcus Overhagen *
15b2859abeSMarcus Overhagen * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16b2859abeSMarcus Overhagen * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17b2859abeSMarcus Overhagen * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18b2859abeSMarcus Overhagen * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19b2859abeSMarcus Overhagen * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20b2859abeSMarcus Overhagen * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21b2859abeSMarcus Overhagen * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22b2859abeSMarcus Overhagen * OTHER DEALINGS IN THE SOFTWARE.
23b2859abeSMarcus Overhagen */
24b2859abeSMarcus Overhagen
25b2859abeSMarcus Overhagen #include <media/MediaFormats.h>
26fcb9bdc0SMarcus Overhagen #include <media/MediaNode.h>
27b2859abeSMarcus Overhagen #include <stdio.h>
28b2859abeSMarcus Overhagen #include "MediaFormat.h"
29b2859abeSMarcus Overhagen
30699dc0e3SMarcus Overhagen extern "C" {
31699dc0e3SMarcus Overhagen #include "avcodec.h"
32366ee548SColin Günther
33366ee548SColin Günther #ifdef DEBUG
34366ee548SColin Günther // Needed to fix debug build, otherwise the linker complains about
35366ee548SColin Günther // "undefined reference to `ff_log2_tab'"
36366ee548SColin Günther const uint8_t ff_log2_tab[256] = {0};
37366ee548SColin Günther #endif
38366ee548SColin Günther
39366ee548SColin Günther } // extern "C"
40b2859abeSMarcus Overhagen
41*d3c8f2b2SJérôme Duval
42*d3c8f2b2SJérôme Duval #if LIBAVCODEC_VERSION_INT <= ((54 << 16) | (50 << 8))
43*d3c8f2b2SJérôme Duval #define AV_CODEC_ID_AC3 CODEC_ID_AC3
44*d3c8f2b2SJérôme Duval #define AV_CODEC_ID_MP3 CODEC_ID_MP3
45*d3c8f2b2SJérôme Duval #define AV_CODEC_ID_MPEG2VIDEO CODEC_ID_MPEG2VIDEO
46*d3c8f2b2SJérôme Duval #endif
47*d3c8f2b2SJérôme Duval
48*d3c8f2b2SJérôme Duval
49b2859abeSMarcus Overhagen void
PrintFormat(const media_format & format)50b2859abeSMarcus Overhagen PrintFormat(const media_format &format)
51b2859abeSMarcus Overhagen {
52b2859abeSMarcus Overhagen char s[200];
53b2859abeSMarcus Overhagen string_for_format(format, s, sizeof(s));
54b2859abeSMarcus Overhagen printf("%s\n", s);
55b2859abeSMarcus Overhagen }
56b2859abeSMarcus Overhagen
57b2859abeSMarcus Overhagen
58b0b3cab0SMarcus Overhagen void
PrintFormat(const media_output & output)59b0b3cab0SMarcus Overhagen PrintFormat(const media_output &output)
60b2859abeSMarcus Overhagen {
61b0b3cab0SMarcus Overhagen PrintFormat(output.format);
62b0b3cab0SMarcus Overhagen }
63b2859abeSMarcus Overhagen
64b2859abeSMarcus Overhagen
65b0b3cab0SMarcus Overhagen static status_t
GetHeaderFormatAc3Audio(media_format * out_format,const uint8 * header,size_t size)66b0b3cab0SMarcus Overhagen GetHeaderFormatAc3Audio(media_format *out_format, const uint8 *header, size_t size)
67b0b3cab0SMarcus Overhagen {
68b0b3cab0SMarcus Overhagen printf("GetHeaderFormatAc3Audio\n");
69b2859abeSMarcus Overhagen
70b0b3cab0SMarcus Overhagen status_t status;
71b2859abeSMarcus Overhagen media_format_description desc;
72699dc0e3SMarcus Overhagen // desc.family = B_WAV_FORMAT_FAMILY;
73699dc0e3SMarcus Overhagen // desc.u.wav.codec = 0x2000;
74699dc0e3SMarcus Overhagen desc.family = B_MISC_FORMAT_FAMILY;
75699dc0e3SMarcus Overhagen desc.u.misc.file_format = 'ffmp';
76*d3c8f2b2SJérôme Duval desc.u.misc.codec = AV_CODEC_ID_AC3;
77b2859abeSMarcus Overhagen
78b2859abeSMarcus Overhagen BMediaFormats formats;
79b0b3cab0SMarcus Overhagen status = formats.InitCheck();
804da4784aSMarcus Overhagen if (status) {
814da4784aSMarcus Overhagen printf("formats.InitCheck failed, error %lu\n", status);
82b0b3cab0SMarcus Overhagen return status;
83b0b3cab0SMarcus Overhagen }
84b2859abeSMarcus Overhagen
854da4784aSMarcus Overhagen status = formats.GetFormatFor(desc, out_format);
864da4784aSMarcus Overhagen if (status) {
874da4784aSMarcus Overhagen printf("formats.GetFormatFor failed, error %lu\n", status);
884da4784aSMarcus Overhagen return status;
894da4784aSMarcus Overhagen }
904da4784aSMarcus Overhagen
914da4784aSMarcus Overhagen return B_OK;
924da4784aSMarcus Overhagen }
934da4784aSMarcus Overhagen
944da4784aSMarcus Overhagen
95b0b3cab0SMarcus Overhagen
96b0b3cab0SMarcus Overhagen static status_t
GetHeaderFormatDtsAudio(media_format * out_format,const uint8 * header,size_t size)97b0b3cab0SMarcus Overhagen GetHeaderFormatDtsAudio(media_format *out_format, const uint8 *header, size_t size)
98b0b3cab0SMarcus Overhagen {
99b0b3cab0SMarcus Overhagen printf("GetHeaderFormatDtsAudio: unsupported\n");
100b0b3cab0SMarcus Overhagen return B_ERROR;
101b0b3cab0SMarcus Overhagen }
102b0b3cab0SMarcus Overhagen
103b0b3cab0SMarcus Overhagen
104b0b3cab0SMarcus Overhagen static status_t
GetHeaderFormatLpcmAudio(media_format * out_format,const uint8 * header,size_t size)105b0b3cab0SMarcus Overhagen GetHeaderFormatLpcmAudio(media_format *out_format, const uint8 *header, size_t size)
106b0b3cab0SMarcus Overhagen {
107b0b3cab0SMarcus Overhagen printf("GetHeaderFormatLpcmAudio: unsupported\n");
108b0b3cab0SMarcus Overhagen return B_ERROR;
109b0b3cab0SMarcus Overhagen }
110b0b3cab0SMarcus Overhagen
111b0b3cab0SMarcus Overhagen
112b0b3cab0SMarcus Overhagen static status_t
GetHeaderFormatPrivateStream(media_format * out_format,const uint8 * header,size_t size)113b0b3cab0SMarcus Overhagen GetHeaderFormatPrivateStream(media_format *out_format, const uint8 *header, size_t size)
114b0b3cab0SMarcus Overhagen {
115b0b3cab0SMarcus Overhagen printf("GetHeaderFormatPrivateStream: unsupported, assuming AC3\n");
116b0b3cab0SMarcus Overhagen return GetHeaderFormatAc3Audio(out_format, header, size);
117b0b3cab0SMarcus Overhagen }
118b0b3cab0SMarcus Overhagen
119b0b3cab0SMarcus Overhagen
120b0b3cab0SMarcus Overhagen static status_t
GetHeaderFormatMpegAudio(media_format * out_format,const uint8 * header,size_t size)121b0b3cab0SMarcus Overhagen GetHeaderFormatMpegAudio(media_format *out_format, const uint8 *header, size_t size)
122b0b3cab0SMarcus Overhagen {
123b0b3cab0SMarcus Overhagen printf("GetHeaderFormatMpegAudio\n");
124b0b3cab0SMarcus Overhagen
125b0b3cab0SMarcus Overhagen status_t status;
126b0b3cab0SMarcus Overhagen media_format_description desc;
127699dc0e3SMarcus Overhagen // desc.family = B_MPEG_FORMAT_FAMILY;
128699dc0e3SMarcus Overhagen // desc.u.mpeg.id = B_MPEG_2_AUDIO_LAYER_2;
129699dc0e3SMarcus Overhagen desc.family = B_MISC_FORMAT_FAMILY;
130699dc0e3SMarcus Overhagen desc.u.misc.file_format = 'ffmp';
131*d3c8f2b2SJérôme Duval desc.u.misc.codec = AV_CODEC_ID_MP3;
132699dc0e3SMarcus Overhagen
133b0b3cab0SMarcus Overhagen
134b0b3cab0SMarcus Overhagen BMediaFormats formats;
135b0b3cab0SMarcus Overhagen status = formats.InitCheck();
1364da4784aSMarcus Overhagen if (status) {
1374da4784aSMarcus Overhagen printf("formats.InitCheck failed, error %lu\n", status);
138b0b3cab0SMarcus Overhagen return status;
1394da4784aSMarcus Overhagen }
140b0b3cab0SMarcus Overhagen
141b0b3cab0SMarcus Overhagen status = formats.GetFormatFor(desc, out_format);
1424da4784aSMarcus Overhagen if (status) {
1434da4784aSMarcus Overhagen printf("formats.GetFormatFor failed, error %lu\n", status);
144b0b3cab0SMarcus Overhagen return status;
1454da4784aSMarcus Overhagen }
146b2859abeSMarcus Overhagen
147b2859abeSMarcus Overhagen out_format->u.encoded_audio.output.frame_rate = 48000;
148b2859abeSMarcus Overhagen out_format->u.encoded_audio.output.channel_count = 2;
149b2859abeSMarcus Overhagen out_format->u.encoded_audio.output.buffer_size = 1024;
150b2859abeSMarcus Overhagen return B_OK;
151b2859abeSMarcus Overhagen }
152b0b3cab0SMarcus Overhagen
153b0b3cab0SMarcus Overhagen
154b0b3cab0SMarcus Overhagen static status_t
GetHeaderFormatMpegVideo(media_format * out_format,const uint8 * header,size_t size)155b0b3cab0SMarcus Overhagen GetHeaderFormatMpegVideo(media_format *out_format, const uint8 *header, size_t size)
156b0b3cab0SMarcus Overhagen {
157b0b3cab0SMarcus Overhagen printf("GetHeaderFormatMpegVideo\n");
158b0b3cab0SMarcus Overhagen
159b0b3cab0SMarcus Overhagen status_t status;
160b0b3cab0SMarcus Overhagen media_format_description desc;
161699dc0e3SMarcus Overhagen // desc.family = B_MPEG_FORMAT_FAMILY;
162699dc0e3SMarcus Overhagen // desc.u.mpeg.id = B_MPEG_2_VIDEO;
163699dc0e3SMarcus Overhagen desc.family = B_MISC_FORMAT_FAMILY;
164699dc0e3SMarcus Overhagen desc.u.misc.file_format = 'ffmp';
165*d3c8f2b2SJérôme Duval desc.u.misc.codec = AV_CODEC_ID_MPEG2VIDEO;
166b0b3cab0SMarcus Overhagen
167b0b3cab0SMarcus Overhagen BMediaFormats formats;
168b0b3cab0SMarcus Overhagen status = formats.InitCheck();
1694da4784aSMarcus Overhagen if (status) {
1704da4784aSMarcus Overhagen printf("formats.InitCheck failed, error %lu\n", status);
171b0b3cab0SMarcus Overhagen return status;
1724da4784aSMarcus Overhagen }
173b0b3cab0SMarcus Overhagen
1744da4784aSMarcus Overhagen status = formats.GetFormatFor(desc, out_format);
1754da4784aSMarcus Overhagen if (status) {
1764da4784aSMarcus Overhagen printf("formats.GetFormatFor failed, error %lu\n", status);
1774da4784aSMarcus Overhagen return status;
1784da4784aSMarcus Overhagen }
1794da4784aSMarcus Overhagen
1804da4784aSMarcus Overhagen return B_OK;
181b0b3cab0SMarcus Overhagen }
182b0b3cab0SMarcus Overhagen
183b0b3cab0SMarcus Overhagen
184b0b3cab0SMarcus Overhagen status_t
GetHeaderFormat(media_format * out_format,const void * header,size_t size,int stream_id)185b0b3cab0SMarcus Overhagen GetHeaderFormat(media_format *out_format, const void *header, size_t size, int stream_id)
186b0b3cab0SMarcus Overhagen {
187b0b3cab0SMarcus Overhagen const uint8 *h = (const uint8 *)header;
188b0b3cab0SMarcus Overhagen status_t status;
189b0b3cab0SMarcus Overhagen
190b0b3cab0SMarcus Overhagen printf("GetHeaderFormat: stream_id %02x\n", stream_id);
191b0b3cab0SMarcus Overhagen printf("inner frame header: "
192b0b3cab0SMarcus Overhagen "%02x %02x %02x %02x %02x %02x %02x %02x "
193b0b3cab0SMarcus Overhagen "%02x %02x %02x %02x %02x %02x %02x %02x\n",
194b0b3cab0SMarcus Overhagen h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7],
195b0b3cab0SMarcus Overhagen h[8], h[9], h[10], h[11], h[12], h[13], h[14], h[15]);
196b0b3cab0SMarcus Overhagen
197b0b3cab0SMarcus Overhagen if (stream_id >= 0x80 && stream_id <= 0x87)
198b0b3cab0SMarcus Overhagen status = GetHeaderFormatAc3Audio(out_format, h, size);
199b0b3cab0SMarcus Overhagen else if (stream_id >= 0x88 && stream_id <= 0x8F)
200b0b3cab0SMarcus Overhagen status = GetHeaderFormatDtsAudio(out_format, h, size);
201b0b3cab0SMarcus Overhagen else if (stream_id >= 0xA0 && stream_id <= 0xA7)
202b0b3cab0SMarcus Overhagen status = GetHeaderFormatLpcmAudio(out_format, h, size);
203b0b3cab0SMarcus Overhagen else if (stream_id == 0xBD)
204b0b3cab0SMarcus Overhagen status = GetHeaderFormatPrivateStream(out_format, h, size);
205b0b3cab0SMarcus Overhagen else if (stream_id >= 0xC0 && stream_id <= 0xDF)
206b0b3cab0SMarcus Overhagen status = GetHeaderFormatMpegAudio(out_format, h, size);
207b0b3cab0SMarcus Overhagen else if (stream_id >= 0xE0 && stream_id <= 0xEF)
208b0b3cab0SMarcus Overhagen status = GetHeaderFormatMpegVideo(out_format, h, size);
209b0b3cab0SMarcus Overhagen else {
210b0b3cab0SMarcus Overhagen printf("GetHeaderFormat: don't know what this stream_id means\n");
211b0b3cab0SMarcus Overhagen status = B_ERROR;
212b0b3cab0SMarcus Overhagen }
213b0b3cab0SMarcus Overhagen
214b0b3cab0SMarcus Overhagen if (status != B_OK) {
215b0b3cab0SMarcus Overhagen printf("GetHeaderFormat: failed!\n");
216b0b3cab0SMarcus Overhagen } else {
217b0b3cab0SMarcus Overhagen printf("GetHeaderFormat: out_format ");
218b0b3cab0SMarcus Overhagen PrintFormat(*out_format);
219b0b3cab0SMarcus Overhagen }
220b0b3cab0SMarcus Overhagen return status;
221b0b3cab0SMarcus Overhagen }
222