comparison libdlna-0.2.3/src/audio_aac.c @ 129:4f6d9621ee00

add multi session streaming & add depending librarys. - libupnp-1.6.6 - libdlna-0.2.3
author Naoya OYAMA <naoya.oyama@gmail.com>
date Sun, 10 Oct 2010 15:33:18 +0900
parents
children
comparison
equal deleted inserted replaced
128:3a7d8d2f0585 129:4f6d9621ee00
1 /*
2 * libdlna: reference DLNA standards implementation.
3 * Copyright (C) 2007 Benjamin Zores <ben@geexbox.org>
4 *
5 * This file is part of libdlna.
6 *
7 * libdlna is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * libdlna is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with libdlna; if not, write to the Free Software
19 * Foundation, Inc, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28
29 #include "dlna_internals.h"
30 #include "profiles.h"
31 #include "containers.h"
32
33 /* Profile for audio media class content */
34 static dlna_profile_t aac_adts = {
35 .id = "AAC_ADTS",
36 .mime = MIME_AUDIO_ADTS,
37 .label = LABEL_AUDIO_2CH
38 };
39
40 /* Profile for audio media class content */
41 static dlna_profile_t aac_adts_320 = {
42 .id = "AAC_ADTS_320",
43 .mime = MIME_AUDIO_ADTS,
44 .label = LABEL_AUDIO_2CH
45 };
46
47 /* Profile for audio media class content */
48 static dlna_profile_t aac_iso = {
49 .id = "AAC_ISO",
50 .mime = MIME_AUDIO_MPEG_4,
51 .label = LABEL_AUDIO_2CH
52 };
53
54 /* Profile for audio media class content */
55 static dlna_profile_t aac_iso_320 = {
56 .id = "AAC_ISO_320",
57 .mime = MIME_AUDIO_MPEG_4,
58 .label = LABEL_AUDIO_2CH
59 };
60
61 /* Profile for audio media class content. In the case of AAC LTP profiles,
62 both the ISO file formats and the ADTS format are supported by
63 the same profile. */
64 static dlna_profile_t aac_ltp_iso = {
65 .id = "AAC_LTP_ISO",
66 .mime = MIME_AUDIO_MPEG_4,
67 .label = LABEL_AUDIO_2CH
68 };
69
70 /* Profile for audio media class content with up to 5.1 channels */
71 static dlna_profile_t aac_ltp_mult5_iso = {
72 .id = "AAC_LTP_MULT5_ISO",
73 .mime = MIME_AUDIO_MPEG_4,
74 .label = LABEL_AUDIO_MULTI
75 };
76
77 /* Profile for audio media class content with up to 7.1 channels */
78 static dlna_profile_t aac_ltp_mult7_iso = {
79 .id = "AAC_LTP_MULT7_ISO",
80 .mime = MIME_AUDIO_MPEG_4,
81 .label = LABEL_AUDIO_MULTI
82 };
83
84 /* Profile for audio media class content with up to 5.1 channels */
85 static dlna_profile_t aac_mult5_adts = {
86 .id = "AAC_MULT5_ADTS",
87 .mime = MIME_AUDIO_ADTS,
88 .label = LABEL_AUDIO_MULTI
89 };
90
91 /* Profile for audio media class content with up to 5.1 channels */
92 static dlna_profile_t aac_mult5_iso = {
93 .id = "AAC_MULT5_ISO",
94 .mime = MIME_AUDIO_MPEG_4,
95 .label = LABEL_AUDIO_MULTI
96 };
97
98 /* Profile for audio media class content */
99 static dlna_profile_t heaac_l2_adts = {
100 .id = "HEAAC_L2_ADTS",
101 .mime = MIME_AUDIO_ADTS,
102 .label = LABEL_AUDIO_2CH
103 };
104
105 /* Profile for audio media class content */
106 static dlna_profile_t heaac_l2_iso = {
107 .id = "HEAAC_L2_ISO",
108 .mime = MIME_AUDIO_MPEG_4,
109 .label = LABEL_AUDIO_2CH
110 };
111
112 /* Profile for audio media class content */
113 static dlna_profile_t heaac_l3_adts = {
114 .id = "HEAAC_L3_ADTS",
115 .mime = MIME_AUDIO_ADTS,
116 .label = LABEL_AUDIO_2CH
117 };
118
119 /* Profile for audio media class content */
120 static dlna_profile_t heaac_l3_iso = {
121 .id = "HEAAC_L3_ISO",
122 .mime = MIME_AUDIO_MPEG_4,
123 .label = LABEL_AUDIO_2CH
124 };
125
126 /* Profile for audio media class content with up to 5.1 channels */
127 static dlna_profile_t heaac_mult5_adts = {
128 .id = "HEAAC_MULT5_ADTS",
129 .mime = MIME_AUDIO_ADTS,
130 .label = LABEL_AUDIO_MULTI
131 };
132
133 /* Profile for audio media class content with up to 5.1 channels */
134 static dlna_profile_t heaac_mult5_iso = {
135 .id = "HEAAC_MULT5_ISO",
136 .mime = MIME_AUDIO_MPEG_4,
137 .label = LABEL_AUDIO_MULTI
138 };
139
140 /* Profile for audio media class content */
141 static dlna_profile_t heaac_l2_adts_320 = {
142 .id = "HEAAC_L2_ADTS_320",
143 .mime = MIME_AUDIO_ADTS,
144 .label = LABEL_AUDIO_2CH
145 };
146
147 /* Profile for audio media class content */
148 static dlna_profile_t heaac_l2_iso_320 = {
149 .id = "HEAAC_L2_ISO_320",
150 .mime = MIME_AUDIO_MPEG_4,
151 .label = LABEL_AUDIO_2CH
152 };
153
154 /* Profile for audio media class content */
155 static dlna_profile_t bsac_iso = {
156 .id = "BSAC_ISO",
157 .mime = MIME_AUDIO_MPEG_4,
158 .label = LABEL_AUDIO_2CH
159 };
160
161 /* Profile for audio media class content with up to 5.1 channels */
162 static dlna_profile_t bsac_mult5_iso = {
163 .id = "BSAC_MULT5_ISO",
164 .mime = MIME_AUDIO_MPEG_4,
165 .label = LABEL_AUDIO_MULTI
166 };
167
168 static dlna_profile_t heaac_v2_l2 = {
169 .id = "HEAACv2_L2",
170 .mime = MIME_AUDIO_MPEG_4,
171 .label = LABEL_AUDIO_2CH
172 };
173
174 static dlna_profile_t heaac_v2_l2_adts = {
175 .id = "HEAACv2_L2",
176 .mime = MIME_AUDIO_ADTS,
177 .label = LABEL_AUDIO_2CH
178 };
179
180 static dlna_profile_t heaac_v2_l2_320 = {
181 .id = "HEAACv2_L2_320",
182 .mime = MIME_AUDIO_MPEG_4,
183 .label = LABEL_AUDIO_2CH
184 };
185
186 static dlna_profile_t heaac_v2_l2_320_adts = {
187 .id = "HEAACv2_L2_320",
188 .mime = MIME_AUDIO_ADTS,
189 .label = LABEL_AUDIO_2CH
190 };
191
192 static dlna_profile_t heaac_v2_l3 = {
193 .id = "HEAACv2_L3",
194 .mime = MIME_AUDIO_MPEG_4,
195 .label = LABEL_AUDIO_2CH
196 };
197
198 static dlna_profile_t heaac_v2_l3_adts = {
199 .id = "HEAACv2_L3",
200 .mime = MIME_AUDIO_ADTS,
201 .label = LABEL_AUDIO_2CH
202 };
203
204 static dlna_profile_t heaac_v2_mult5 = {
205 .id = "HEAACv2_MULT5",
206 .mime = MIME_AUDIO_MPEG_4,
207 .label = LABEL_AUDIO_2CH
208 };
209
210 static dlna_profile_t heaac_v2_mult5_adts = {
211 .id = "HEAACv2_MULT5",
212 .mime = MIME_AUDIO_ADTS,
213 .label = LABEL_AUDIO_2CH
214 };
215
216 typedef enum {
217 AAC_MUXED, /* AAC is muxed in a container */
218 AAC_RAW /* AAC is raw (ADTS) */
219 } aac_container_type_t;
220
221 /* HeAACv1 (a.k.a. AACplus v1) is AAC LC + Spectral Band Replication (SBR) */
222 /* HeAACv2 (a.ka.a AACplus v2) is HeAACv1 + Parametric Stereo (PS) */
223
224 typedef enum {
225 AAC_INVALID = 0,
226 AAC_MAIN = 1, /* AAC Main */
227 AAC_LC = 2, /* AAC Low complexity */
228 AAC_SSR = 3, /* AAC SSR */
229 AAC_LTP = 4, /* AAC Long term prediction */
230 AAC_HE = 5, /* AAC High efficiency (SBR) */
231 AAC_SCALE = 6, /* Scalable */
232 AAC_TWINVQ = 7, /* TwinVQ */
233 AAC_CELP = 8, /* CELP */
234 AAC_HVXC = 9, /* HVXC */
235 AAC_TTSI = 12, /* TTSI */
236 AAC_MS = 13, /* Main synthetic */
237 AAC_WAVE = 14, /* Wavetable synthesis */
238 AAC_MIDI = 15, /* General MIDI */
239 AAC_FX = 16, /* Algorithmic Synthesis and Audio FX */
240 AAC_LC_ER = 17, /* AAC Low complexity with error recovery */
241 AAC_LTP_ER = 19, /* AAC Long term prediction with error recovery */
242 AAC_SCALE_ER = 20, /* AAC scalable with error recovery */
243 AAC_TWINVQ_ER = 21, /* TwinVQ with error recovery */
244 AAC_BSAC_ER = 22, /* BSAC with error recovery */
245 AAC_LD_ER = 23, /* AAC LD with error recovery */
246 AAC_CELP_ER = 24, /* CELP with error recovery */
247 AAC_HXVC_ER = 25, /* HXVC with error recovery */
248 AAC_HILN_ER = 26, /* HILN with error recovery */
249 AAC_PARAM_ER = 27, /* Parametric with error recovery */
250 AAC_SSC = 28, /* AAC SSC */
251 AAC_HE_L3 = 31, /* Reserved : seems to be HeAAC L3 */
252 } aac_object_type_t;
253
254 static const struct {
255 dlna_profile_t *profile;
256 aac_container_type_t ct;
257 audio_profile_t ap;
258 } aac_profiles_mapping[] = {
259 { &aac_adts, AAC_RAW, AUDIO_PROFILE_AAC },
260 { &aac_adts_320, AAC_RAW, AUDIO_PROFILE_AAC_320 },
261 { &aac_iso, AAC_MUXED, AUDIO_PROFILE_AAC },
262 { &aac_iso_320, AAC_MUXED, AUDIO_PROFILE_AAC_320 },
263 { &aac_ltp_iso, AAC_MUXED, AUDIO_PROFILE_AAC_LTP },
264 { &aac_ltp_mult5_iso, AAC_MUXED, AUDIO_PROFILE_AAC_LTP_MULT5 },
265 { &aac_ltp_mult7_iso, AAC_MUXED, AUDIO_PROFILE_AAC_LTP_MULT7 },
266 { &aac_mult5_adts, AAC_RAW, AUDIO_PROFILE_AAC_MULT5 },
267 { &aac_mult5_iso, AAC_MUXED, AUDIO_PROFILE_AAC_MULT5 },
268 { &heaac_l2_adts, AAC_RAW, AUDIO_PROFILE_AAC_HE_L2 },
269 { &heaac_l2_iso, AAC_MUXED, AUDIO_PROFILE_AAC_HE_L2 },
270 { &heaac_l3_adts, AAC_RAW, AUDIO_PROFILE_AAC_HE_L3 },
271 { &heaac_l3_iso, AAC_MUXED, AUDIO_PROFILE_AAC_HE_L3 },
272 { &heaac_mult5_adts, AAC_RAW, AUDIO_PROFILE_AAC_HE_MULT5 },
273 { &heaac_mult5_iso, AAC_MUXED, AUDIO_PROFILE_AAC_HE_MULT5 },
274 { &heaac_l2_adts_320, AAC_RAW, AUDIO_PROFILE_AAC_HE_L2_320 },
275 { &heaac_l2_iso_320, AAC_MUXED, AUDIO_PROFILE_AAC_HE_L2_320 },
276 { &heaac_v2_l2, AAC_MUXED, AUDIO_PROFILE_AAC_HE_V2_L2 },
277 { &heaac_v2_l2_adts, AAC_RAW, AUDIO_PROFILE_AAC_HE_V2_L2 },
278 { &heaac_v2_l2_320, AAC_MUXED, AUDIO_PROFILE_AAC_HE_V2_L2_320 },
279 { &heaac_v2_l2_320_adts, AAC_RAW, AUDIO_PROFILE_AAC_HE_V2_L2_320 },
280 { &heaac_v2_l3, AAC_MUXED, AUDIO_PROFILE_AAC_HE_V2_L3 },
281 { &heaac_v2_l3_adts, AAC_RAW, AUDIO_PROFILE_AAC_HE_V2_L3 },
282 { &heaac_v2_mult5, AAC_MUXED, AUDIO_PROFILE_AAC_HE_V2_MULT5 },
283 { &heaac_v2_mult5_adts, AAC_RAW, AUDIO_PROFILE_AAC_HE_V2_MULT5 },
284 { &bsac_iso, AAC_MUXED, AUDIO_PROFILE_AAC_BSAC },
285 { &bsac_mult5_iso, AAC_MUXED, AUDIO_PROFILE_AAC_BSAC_MULT5 },
286 { NULL, 0, 0 }
287 };
288
289 static aac_object_type_t
290 aac_object_type_get (uint8_t *data, int len)
291 {
292 uint8_t t = AAC_INVALID;
293
294 if (!data || len < 1)
295 goto aac_object_type_error;
296
297 t = data[0] >> 3; /* Get 5 first bits */
298
299 aac_object_type_error:
300 #ifdef HAVE_DEBUG
301 fprintf (stderr, "AAC Object Type: %d\n", t);
302 #endif /* HAVE_DEBUG */
303
304 return t;
305 }
306
307 static audio_profile_t
308 audio_profile_guess_aac_priv (AVCodecContext *ac, aac_object_type_t type)
309 {
310 if (!ac)
311 return AUDIO_PROFILE_INVALID;
312
313 /* check for AAC variants codec */
314 if (ac->codec_id != CODEC_ID_AAC)
315 return AUDIO_PROFILE_INVALID;
316
317 switch (type)
318 {
319 /* AAC Low Complexity variants */
320 case AAC_LC:
321 case AAC_LC_ER:
322 {
323 if (ac->sample_rate < 8000 || ac->sample_rate > 48000)
324 break;
325
326 if (ac->channels <= 2) /* AAC @ Level 1/2 */
327 {
328 if (ac->bit_rate <= 320000)
329 return AUDIO_PROFILE_AAC_320;
330
331 if (ac->bit_rate <= 576000)
332 return AUDIO_PROFILE_AAC;
333
334 break;
335 }
336 else if (ac->channels <= 6)
337 {
338 if (ac->bit_rate <= 1440000)
339 return AUDIO_PROFILE_AAC_MULT5;
340
341 break;
342 }
343 break;
344 }
345
346 /* AAC Long Term Prediction variants */
347 case AAC_LTP:
348 case AAC_LTP_ER:
349 {
350 if (ac->sample_rate < 8000)
351 break;
352
353 if (ac->sample_rate <= 48000)
354 {
355 if (ac->channels <= 2 && ac->bit_rate <= 576000)
356 return AUDIO_PROFILE_AAC_LTP;
357
358 break;
359 }
360 else if (ac->sample_rate <= 96000)
361 {
362 if (ac->channels <= 6 && ac->bit_rate <= 2880000)
363 return AUDIO_PROFILE_AAC_LTP_MULT5;
364
365 if (ac->channels <= 8 && ac->bit_rate <= 4032000)
366 return AUDIO_PROFILE_AAC_LTP_MULT7;
367
368 break;
369 }
370
371 break;
372 }
373
374 /* AAC High efficiency (with SBR) variants */
375 case AAC_HE:
376 case AAC_HE_L3:
377 {
378 if (ac->sample_rate < 8000)
379 break;
380
381 if (ac->sample_rate <= 24000) /* HE-AAC @ Level 2 */
382 {
383 if (ac->channels > 2)
384 break;
385
386 if (ac->bit_rate <= 320000)
387 return AUDIO_PROFILE_AAC_HE_L2_320;
388
389 if (ac->bit_rate <= 576000)
390 return AUDIO_PROFILE_AAC_HE_L2;
391
392 break;
393 }
394 else if (ac->sample_rate <= 48000)
395 {
396 /* HE-AAC @ Level 3 */
397 if (ac->channels <= 2 && ac->bit_rate <= 576000)
398 return AUDIO_PROFILE_AAC_HE_L3;
399
400 /* HE-AAC @ Level 4/5 */
401 if (ac->channels <= 6 && ac->bit_rate <= 1440000)
402 return AUDIO_PROFILE_AAC_HE_MULT5;
403
404 break;
405 }
406
407 break;
408 }
409
410 /* AAC High efficiency v2 (with PS) variants */
411 case AAC_PARAM_ER:
412 {
413 if (ac->sample_rate < 8000)
414 break;
415
416 if (ac->sample_rate <= 24000) /* HE-AAC @ Level 2 */
417 {
418 if (ac->channels > 2)
419 break;
420
421 if (ac->bit_rate <= 320000)
422 return AUDIO_PROFILE_AAC_HE_V2_L2_320;
423
424 if (ac->bit_rate <= 576000)
425 return AUDIO_PROFILE_AAC_HE_V2_L2;
426
427 break;
428 }
429 else if (ac->sample_rate <= 48000)
430 {
431 /* HE-AAC @ Level 3 */
432 if (ac->channels <= 2 && ac->bit_rate <= 576000)
433 return AUDIO_PROFILE_AAC_HE_V2_L3;
434
435 /* HE-AAC @ Level 4/5 */
436 if (ac->channels <= 6 && ac->bit_rate <= 2880000)
437 return AUDIO_PROFILE_AAC_HE_V2_MULT5;
438
439 break;
440 }
441
442 break;
443 }
444
445 case AAC_BSAC_ER:
446 {
447 if (ac->sample_rate < 16000 || ac->sample_rate > 48000)
448 break;
449
450 if (ac->bit_rate > 128000)
451 break;
452
453 if (ac->channels <= 2)
454 return AUDIO_PROFILE_AAC_BSAC;
455 else if (ac->channels <= 6)
456 return AUDIO_PROFILE_AAC_BSAC_MULT5;
457
458 break;
459 }
460
461 default:
462 break;
463 }
464
465 return AUDIO_PROFILE_INVALID;
466 }
467
468 audio_profile_t
469 audio_profile_guess_aac (AVCodecContext *ac)
470 {
471 aac_object_type_t type;
472
473 if (!ac)
474 return AUDIO_PROFILE_INVALID;
475
476 type = aac_object_type_get (ac->extradata, ac->extradata_size);
477 return audio_profile_guess_aac_priv (ac, type);
478 }
479
480 static aac_object_type_t
481 aac_adts_object_type_get (AVFormatContext *ctx)
482 {
483 int fd;
484 unsigned char buf[4];
485 uint8_t t = AAC_INVALID;
486
487 if (!ctx)
488 return t;
489
490 fd = open (ctx->filename, O_RDONLY);
491 read (fd, buf, sizeof (buf) - 1);
492 t = (buf[2] & 0xC0) >> 6;
493 close (fd);
494
495 #ifdef HAVE_DEBUG
496 fprintf (stderr, "AAC Object Type: %d\n", t);
497 #endif /* HAVE_DEBUG */
498
499 return t;
500 }
501
502 static aac_container_type_t
503 aac_get_format (AVFormatContext *ctx)
504 {
505 int fd;
506 unsigned char buf[4];
507 aac_container_type_t ct = AAC_MUXED;
508
509 if (!ctx)
510 return ct;
511
512 fd = open (ctx->filename, O_RDONLY);
513 read (fd, buf, sizeof (buf) - 1);
514 if ((buf[0] == 0xFF) && ((buf[1] & 0xF6) == 0xF0))
515 {
516 ct = AAC_RAW;
517 #ifdef HAVE_DEBUG
518 fprintf (stderr, "AAC has an ADTS header\n");
519 #endif /* HAVE_DEBUG */
520 }
521 close (fd);
522
523 return ct;
524 }
525
526 static dlna_profile_t *
527 probe_mpeg4 (AVFormatContext *ctx,
528 dlna_container_type_t st,
529 av_codecs_t *codecs)
530 {
531 audio_profile_t ap;
532 aac_container_type_t ct = AAC_MUXED;
533 int i;
534
535 if (!stream_ctx_is_audio (codecs))
536 return NULL;
537
538 /* check for ADTS */
539 if (st == CT_AAC)
540 {
541 aac_object_type_t ot;
542 ct = aac_get_format (ctx);
543 ot = aac_adts_object_type_get (ctx);
544 ap = audio_profile_guess_aac_priv (codecs->ac, ot);
545 }
546 else
547 ap = audio_profile_guess_aac (codecs->ac);
548
549 if (ap == AUDIO_PROFILE_INVALID)
550 return NULL;
551
552 /* find profile according to container type and audio profiles */
553 for (i = 0; aac_profiles_mapping[i].profile; i++)
554 if (aac_profiles_mapping[i].ct == ct &&
555 aac_profiles_mapping[i].ap == ap)
556 return aac_profiles_mapping[i].profile;
557
558 return NULL;
559 }
560
561 dlna_registered_profile_t dlna_profile_audio_mpeg4 = {
562 .id = DLNA_PROFILE_AUDIO_MPEG4,
563 .class = DLNA_CLASS_AUDIO,
564 .extensions = "aac,adts,3gp,mp4,mov,qt,m4a",
565 .probe = probe_mpeg4,
566 .next = NULL
567 };