1 /* 2 * Copyright (c) 2004-2010 Marcus Overhagen <marcus@overhagen.de> 3 * 4 * Permission is hereby granted, free of charge, to any person 5 * obtaining a copy of this software and associated documentation 6 * files (the "Software"), to deal in the Software without restriction, 7 * including without limitation the rights to use, copy, modify, 8 * merge, publish, distribute, sublicense, and/or sell copies of 9 * the Software, and to permit persons to whom the Software is 10 * furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be 13 * included in all copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 17 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 * OTHER DEALINGS IN THE SOFTWARE. 23 */ 24 25 #include <media/MediaFormats.h> 26 #include <media/MediaNode.h> 27 #include <stdio.h> 28 #include "MediaFormat.h" 29 30 extern "C" { 31 #include "avcodec.h" 32 33 #ifdef DEBUG 34 // Needed to fix debug build, otherwise the linker complains about 35 // "undefined reference to `ff_log2_tab'" 36 const uint8_t ff_log2_tab[256] = {0}; 37 #endif 38 39 } // extern "C" 40 41 void 42 PrintFormat(const media_format &format) 43 { 44 char s[200]; 45 string_for_format(format, s, sizeof(s)); 46 printf("%s\n", s); 47 } 48 49 50 void 51 PrintFormat(const media_output &output) 52 { 53 PrintFormat(output.format); 54 } 55 56 57 static status_t 58 GetHeaderFormatAc3Audio(media_format *out_format, const uint8 *header, size_t size) 59 { 60 printf("GetHeaderFormatAc3Audio\n"); 61 62 status_t status; 63 media_format_description desc; 64 // desc.family = B_WAV_FORMAT_FAMILY; 65 // desc.u.wav.codec = 0x2000; 66 desc.family = B_MISC_FORMAT_FAMILY; 67 desc.u.misc.file_format = 'ffmp'; 68 desc.u.misc.codec = CODEC_ID_AC3; 69 70 BMediaFormats formats; 71 status = formats.InitCheck(); 72 if (status) { 73 printf("formats.InitCheck failed, error %lu\n", status); 74 return status; 75 } 76 77 status = formats.GetFormatFor(desc, out_format); 78 if (status) { 79 printf("formats.GetFormatFor failed, error %lu\n", status); 80 return status; 81 } 82 83 return B_OK; 84 } 85 86 87 88 static status_t 89 GetHeaderFormatDtsAudio(media_format *out_format, const uint8 *header, size_t size) 90 { 91 printf("GetHeaderFormatDtsAudio: unsupported\n"); 92 return B_ERROR; 93 } 94 95 96 static status_t 97 GetHeaderFormatLpcmAudio(media_format *out_format, const uint8 *header, size_t size) 98 { 99 printf("GetHeaderFormatLpcmAudio: unsupported\n"); 100 return B_ERROR; 101 } 102 103 104 static status_t 105 GetHeaderFormatPrivateStream(media_format *out_format, const uint8 *header, size_t size) 106 { 107 printf("GetHeaderFormatPrivateStream: unsupported, assuming AC3\n"); 108 return GetHeaderFormatAc3Audio(out_format, header, size); 109 } 110 111 112 static status_t 113 GetHeaderFormatMpegAudio(media_format *out_format, const uint8 *header, size_t size) 114 { 115 printf("GetHeaderFormatMpegAudio\n"); 116 117 status_t status; 118 media_format_description desc; 119 // desc.family = B_MPEG_FORMAT_FAMILY; 120 // desc.u.mpeg.id = B_MPEG_2_AUDIO_LAYER_2; 121 desc.family = B_MISC_FORMAT_FAMILY; 122 desc.u.misc.file_format = 'ffmp'; 123 desc.u.misc.codec = CODEC_ID_MP3; 124 125 126 BMediaFormats formats; 127 status = formats.InitCheck(); 128 if (status) { 129 printf("formats.InitCheck failed, error %lu\n", status); 130 return status; 131 } 132 133 status = formats.GetFormatFor(desc, out_format); 134 if (status) { 135 printf("formats.GetFormatFor failed, error %lu\n", status); 136 return status; 137 } 138 139 out_format->u.encoded_audio.output.frame_rate = 48000; 140 out_format->u.encoded_audio.output.channel_count = 2; 141 out_format->u.encoded_audio.output.buffer_size = 1024; 142 return B_OK; 143 } 144 145 146 static status_t 147 GetHeaderFormatMpegVideo(media_format *out_format, const uint8 *header, size_t size) 148 { 149 printf("GetHeaderFormatMpegVideo\n"); 150 151 status_t status; 152 media_format_description desc; 153 // desc.family = B_MPEG_FORMAT_FAMILY; 154 // desc.u.mpeg.id = B_MPEG_2_VIDEO; 155 desc.family = B_MISC_FORMAT_FAMILY; 156 desc.u.misc.file_format = 'ffmp'; 157 desc.u.misc.codec = CODEC_ID_MPEG2VIDEO; 158 159 BMediaFormats formats; 160 status = formats.InitCheck(); 161 if (status) { 162 printf("formats.InitCheck failed, error %lu\n", status); 163 return status; 164 } 165 166 status = formats.GetFormatFor(desc, out_format); 167 if (status) { 168 printf("formats.GetFormatFor failed, error %lu\n", status); 169 return status; 170 } 171 172 return B_OK; 173 } 174 175 176 status_t 177 GetHeaderFormat(media_format *out_format, const void *header, size_t size, int stream_id) 178 { 179 const uint8 *h = (const uint8 *)header; 180 status_t status; 181 182 printf("GetHeaderFormat: stream_id %02x\n", stream_id); 183 printf("inner frame header: " 184 "%02x %02x %02x %02x %02x %02x %02x %02x " 185 "%02x %02x %02x %02x %02x %02x %02x %02x\n", 186 h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7], 187 h[8], h[9], h[10], h[11], h[12], h[13], h[14], h[15]); 188 189 if (stream_id >= 0x80 && stream_id <= 0x87) 190 status = GetHeaderFormatAc3Audio(out_format, h, size); 191 else if (stream_id >= 0x88 && stream_id <= 0x8F) 192 status = GetHeaderFormatDtsAudio(out_format, h, size); 193 else if (stream_id >= 0xA0 && stream_id <= 0xA7) 194 status = GetHeaderFormatLpcmAudio(out_format, h, size); 195 else if (stream_id == 0xBD) 196 status = GetHeaderFormatPrivateStream(out_format, h, size); 197 else if (stream_id >= 0xC0 && stream_id <= 0xDF) 198 status = GetHeaderFormatMpegAudio(out_format, h, size); 199 else if (stream_id >= 0xE0 && stream_id <= 0xEF) 200 status = GetHeaderFormatMpegVideo(out_format, h, size); 201 else { 202 printf("GetHeaderFormat: don't know what this stream_id means\n"); 203 status = B_ERROR; 204 } 205 206 if (status != B_OK) { 207 printf("GetHeaderFormat: failed!\n"); 208 } else { 209 printf("GetHeaderFormat: out_format "); 210 PrintFormat(*out_format); 211 } 212 return status; 213 } 214