BVB Source Codes

cmus Show aac.c Source code

Return Download cmus: download aac.c Source code - Download cmus Source code - Type:.c
  1. /*
  2.  * Copyright 2008-2013 Various Authors
  3.  * Copyright 2006 dnk <dnk@bjum.net>
  4.  *
  5.  * This program is free software; you can redistribute it and/or
  6.  * modify it under the terms of the GNU General Public License as
  7.  * published by the Free Software Foundation; either version 2 of the
  8.  * License, or (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful, but
  11.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, see <http://www.gnu.org/licenses/>.
  17.  */
  18.  
  19. #include "ip.h"
  20. #include "xmalloc.h"
  21. #include "debug.h"
  22. #include "id3.h"
  23. #include "comment.h"
  24. #include "read_wrapper.h"
  25. #include "aac.h"
  26.  
  27. #include <neaacdec.h>
  28.  
  29. #include <stdio.h>
  30. #include <errno.h>
  31. #include <string.h>
  32. #include <sys/types.h>
  33. #include <unistd.h>
  34. #include <fcntl.h>
  35.  
  36. /* FAAD_MIN_STREAMSIZE == 768, 6 == # of channels */
  37. #define BUFFER_SIZE     (FAAD_MIN_STREAMSIZE * 6 * 4)
  38.  
  39. struct aac_private {
  40.         char rbuf[BUFFER_SIZE];
  41.         int rbuf_len;
  42.         int rbuf_pos;
  43.  
  44.         unsigned char channels;
  45.         unsigned long sample_rate;
  46.         long bitrate;
  47.         int object_type;
  48.  
  49.         struct {
  50.                 unsigned long samples;
  51.                 unsigned long bytes;
  52.         } current;
  53.  
  54.         char *overflow_buf;
  55.         int overflow_buf_len;
  56.  
  57.         NeAACDecHandle decoder; /* typedef void * */
  58. };
  59.  
  60. static inline int buffer_length(const struct input_plugin_data *ip_data)
  61. {
  62.         struct aac_private *priv = ip_data->private;
  63.  
  64.         return priv->rbuf_len - priv->rbuf_pos;
  65. }
  66.  
  67. static inline void *buffer_data(const struct input_plugin_data *ip_data)
  68. {
  69.         struct aac_private *priv = ip_data->private;
  70.  
  71.         return priv->rbuf + priv->rbuf_pos;
  72. }
  73.  
  74. static int buffer_fill(struct input_plugin_data *ip_data)
  75. {
  76.         struct aac_private *priv = ip_data->private;
  77.         int32_t n;
  78.  
  79.         if (priv->rbuf_pos > 0) {
  80.                 priv->rbuf_len = buffer_length(ip_data);
  81.                 memmove(priv->rbuf, priv->rbuf + priv->rbuf_pos, priv->rbuf_len);
  82.                 priv->rbuf_pos = 0;
  83.         }
  84.  
  85.         if (priv->rbuf_len == BUFFER_SIZE)
  86.                 return 1;
  87.  
  88.         n = read_wrapper(ip_data, priv->rbuf + priv->rbuf_len, BUFFER_SIZE - priv->rbuf_len);
  89.         if (n == -1)
  90.                 return -1;
  91.         if (n == 0)
  92.                 return 0;
  93.  
  94.         priv->rbuf_len += n;
  95.         return 1;
  96. }
  97.  
  98. static inline void buffer_consume(struct input_plugin_data *ip_data, int n)
  99. {
  100.         struct aac_private *priv = ip_data->private;
  101.  
  102.         BUG_ON(n > buffer_length(ip_data));
  103.  
  104.         priv->rbuf_pos += n;
  105. }
  106.  
  107. static int buffer_fill_min(struct input_plugin_data *ip_data, int len)
  108. {
  109.         int rc;
  110.  
  111.         BUG_ON(len > BUFFER_SIZE);
  112.  
  113.         while (buffer_length(ip_data) < len) {
  114.                 rc = buffer_fill(ip_data);
  115.                 if (rc <= 0)
  116.                         return rc;
  117.         }
  118.         return 1;
  119. }
  120.  
  121. /* 'data' must point to at least 6 bytes of data */
  122. static inline int parse_frame(const unsigned char data[6])
  123. {
  124.         int len;
  125.  
  126.         /* http://www.audiocoding.com/modules/wiki/?page=ADTS */
  127.  
  128.         /* first 12 bits must be set */
  129.         if (data[0] != 0xFF)
  130.                 return 0;
  131.         if ((data[1] & 0xF0) != 0xF0)
  132.                 return 0;
  133.  
  134.         /* layer is always '00' */
  135.         if ((data[1] & 0x06) != 0x00)
  136.                 return 0;
  137.  
  138.         /* frame length is stored in 13 bits */
  139.         len  = data[3] << 11;   /* ..1100000000000 */
  140.         len |= data[4] << 3;    /* ..xx11111111xxx */
  141.         len |= data[5] >> 5;    /* ..xxxxxxxxxx111 */
  142.         len &= 0x1FFF;          /* 13 bits */
  143.         return len;
  144. }
  145.  
  146. /* scans forward to the next aac frame and makes sure
  147.  * the entire frame is in the buffer.
  148.  */
  149. static int buffer_fill_frame(struct input_plugin_data *ip_data)
  150. {
  151.         unsigned char *data;
  152.         int rc, n, len;
  153.         int max = 32768;
  154.  
  155.         while (1) {
  156.                 /* need at least 6 bytes of data */
  157.                 rc = buffer_fill_min(ip_data, 6);
  158.                 if (rc <= 0)
  159.                         return rc;
  160.  
  161.                 len = buffer_length(ip_data);
  162.                 data = buffer_data(ip_data);
  163.  
  164.                 /* scan for a frame */
  165.                 for (n = 0; n < len - 5; n++) {
  166.                         /* give up after 32KB */
  167.                         if (max-- == 0) {
  168.                                 d_print("no frame found!\n");
  169.                                 /* FIXME: set errno? */
  170.                                 return -1;
  171.                         }
  172.  
  173.                         /* see if there's a frame at this location */
  174.                         rc = parse_frame(data + n);
  175.                         if (rc == 0)
  176.                                 continue;
  177.  
  178.                         /* found a frame, consume all data up to the frame */
  179.                         buffer_consume(ip_data, n);
  180.  
  181.                         /* rc == frame length */
  182.                         rc = buffer_fill_min(ip_data, rc);
  183.                         if (rc <= 0)
  184.                                 return rc;
  185.  
  186.                         return 1;
  187.                 }
  188.  
  189.                 /* consume what we used */
  190.                 buffer_consume(ip_data, n);
  191.         }
  192.         /* not reached */
  193. }
  194.  
  195. static void aac_get_channel_map(struct input_plugin_data *ip_data)
  196. {
  197.         struct aac_private *priv = ip_data->private;
  198.         NeAACDecFrameInfo frame_info;
  199.         void *buf;
  200.         int i;
  201.  
  202.         ip_data->channel_map[0] = CHANNEL_POSITION_INVALID;
  203.  
  204.         if (buffer_fill_frame(ip_data) <= 0)
  205.                 return;
  206.  
  207.         buf = NeAACDecDecode(priv->decoder, &frame_info, buffer_data(ip_data), buffer_length(ip_data));
  208.         if (!buf || frame_info.error != 0 || frame_info.bytesconsumed <= 0
  209.                         || frame_info.channels > CHANNELS_MAX)
  210.                 return;
  211.  
  212.         for (i = 0; i < frame_info.channels; i++)
  213.                 ip_data->channel_map[i] = channel_position_aac(frame_info.channel_position[i]);
  214. }
  215.  
  216. static int aac_open(struct input_plugin_data *ip_data)
  217. {
  218.         struct aac_private *priv;
  219.         NeAACDecConfigurationPtr neaac_cfg;
  220.         int ret, n;
  221.  
  222.         /* init private struct */
  223.         const struct aac_private priv_init = {
  224.                 .decoder = NeAACDecOpen(),
  225.                 .bitrate = -1,
  226.                 .object_type = -1
  227.         };
  228.         priv = xnew(struct aac_private, 1);
  229.         *priv = priv_init;
  230.         ip_data->private = priv;
  231.  
  232.         /* set decoder config */
  233.         neaac_cfg = NeAACDecGetCurrentConfiguration(priv->decoder);
  234.         neaac_cfg->outputFormat = FAAD_FMT_16BIT;       /* force 16 bit audio */
  235.         neaac_cfg->downMatrix = 0;                      /* NOT 5.1 -> stereo */
  236.         neaac_cfg->dontUpSampleImplicitSBR = 0;         /* upsample, please! */
  237.         NeAACDecSetConfiguration(priv->decoder, neaac_cfg);
  238.  
  239.         /* find a frame */
  240.         if (buffer_fill_frame(ip_data) <= 0) {
  241.                 ret = -IP_ERROR_FILE_FORMAT;
  242.                 goto out;
  243.         }
  244.  
  245.         /* in case of a bug, make sure there is at least some data
  246.          * in the buffer for NeAACDecInit() to work with.
  247.          */
  248.         if (buffer_fill_min(ip_data, 256) <= 0) {
  249.                 d_print("not enough data\n");
  250.                 ret = -IP_ERROR_FILE_FORMAT;
  251.                 goto out;
  252.         }
  253.  
  254.         /* init decoder, returns the length of the header (if any) */
  255.         n = NeAACDecInit(priv->decoder, buffer_data(ip_data), buffer_length(ip_data),
  256.                 &priv->sample_rate, &priv->channels);
  257.         if (n < 0) {
  258.                 d_print("NeAACDecInit failed\n");
  259.                 ret = -IP_ERROR_FILE_FORMAT;
  260.                 goto out;
  261.         }
  262.  
  263.         d_print("sample rate %luhz, channels %u\n", priv->sample_rate, priv->channels);
  264.         if (!priv->sample_rate || !priv->channels) {
  265.                 ret = -IP_ERROR_FILE_FORMAT;
  266.                 goto out;
  267.         }
  268.  
  269.         /* skip the header */
  270.         d_print("skipping header (%d bytes)\n", n);
  271.  
  272.         buffer_consume(ip_data, n);
  273.  
  274.         /*NeAACDecInitDRM(priv->decoder, priv->sample_rate, priv->channels);*/
  275.  
  276.         ip_data->sf = sf_rate(priv->sample_rate) | sf_channels(priv->channels) | sf_bits(16) | sf_signed(1);
  277.         ip_data->sf |= sf_host_endian();
  278.         aac_get_channel_map(ip_data);
  279.  
  280.         return 0;
  281. out:
  282.         NeAACDecClose(priv->decoder);
  283.         free(priv);
  284.         return ret;
  285. }
  286.  
  287. static int aac_close(struct input_plugin_data *ip_data)
  288. {
  289.         struct aac_private *priv = ip_data->private;
  290.  
  291.         NeAACDecClose(priv->decoder);
  292.         free(priv);
  293.         ip_data->private = NULL;
  294.         return 0;
  295. }
  296.  
  297. /* returns -1 on fatal errors
  298.  * returns -2 on non-fatal errors
  299.  * 0 on eof
  300.  * number of bytes put in 'buffer' on success */
  301. static int decode_one_frame(struct input_plugin_data *ip_data, void *buffer, int count)
  302. {
  303.         struct aac_private *priv = ip_data->private;
  304.         unsigned char *aac_data;
  305.         unsigned int aac_data_size;
  306.         NeAACDecFrameInfo frame_info;
  307.         char *sample_buf;
  308.         int bytes, rc;
  309.  
  310.         rc = buffer_fill_frame(ip_data);
  311.         if (rc <= 0)
  312.                 return rc;
  313.  
  314.         aac_data = buffer_data(ip_data);
  315.         aac_data_size = buffer_length(ip_data);
  316.  
  317.         /* aac data -> raw pcm */
  318.         sample_buf = NeAACDecDecode(priv->decoder, &frame_info, aac_data, aac_data_size);
  319.         if (frame_info.error == 0 && frame_info.samples > 0) {
  320.                 priv->current.samples += frame_info.samples;
  321.                 priv->current.bytes += frame_info.bytesconsumed;
  322.         }
  323.  
  324.         buffer_consume(ip_data, frame_info.bytesconsumed);
  325.  
  326.         if (!sample_buf || frame_info.bytesconsumed <= 0) {
  327.                 d_print("fatal error: %s\n", NeAACDecGetErrorMessage(frame_info.error));
  328.                 errno = EINVAL;
  329.                 return -1;
  330.         }
  331.  
  332.         if (frame_info.error != 0) {
  333.                 d_print("frame error: %s\n", NeAACDecGetErrorMessage(frame_info.error));
  334.                 return -2;
  335.         }
  336.  
  337.         if (frame_info.samples <= 0)
  338.                 return -2;
  339.  
  340.         if (frame_info.channels != priv->channels || frame_info.samplerate != priv->sample_rate) {
  341.                 d_print("invalid channel or sample_rate count\n");
  342.                 return -2;
  343.         }
  344.  
  345.         /* 16-bit samples */
  346.         bytes = frame_info.samples * 2;
  347.  
  348.         if (bytes > count) {
  349.                 /* decoded too much, keep overflow */
  350.                 priv->overflow_buf = sample_buf + count;
  351.                 priv->overflow_buf_len = bytes - count;
  352.                 memcpy(buffer, sample_buf, count);
  353.                 return count;
  354.         } else {
  355.                 memcpy(buffer, sample_buf, bytes);
  356.         }
  357.         return bytes;
  358. }
  359.  
  360. static int aac_read(struct input_plugin_data *ip_data, char *buffer, int count)
  361. {
  362.         struct aac_private *priv = ip_data->private;
  363.         int rc;
  364.  
  365.         /* use overflow from previous call (if any) */
  366.         if (priv->overflow_buf_len) {
  367.                 int len = priv->overflow_buf_len;
  368.  
  369.                 if (len > count)
  370.                         len = count;
  371.  
  372.                 memcpy(buffer, priv->overflow_buf, len);
  373.                 priv->overflow_buf += len;
  374.                 priv->overflow_buf_len -= len;
  375.                 return len;
  376.         }
  377.  
  378.         do {
  379.                 rc = decode_one_frame(ip_data, buffer, count);
  380.         } while (rc == -2);
  381.         return rc;
  382. }
  383.  
  384. static int aac_seek(struct input_plugin_data *ip_data, double offset)
  385. {
  386.         return -IP_ERROR_FUNCTION_NOT_SUPPORTED;
  387. }
  388.  
  389. static int aac_read_comments(struct input_plugin_data *ip_data,
  390.                 struct keyval **comments)
  391. {
  392.         GROWING_KEYVALS(c);
  393.         struct id3tag id3;
  394.         int rc, fd, i;
  395.  
  396.         fd = open(ip_data->filename, O_RDONLY);
  397.         if (fd == -1)
  398.                 return -1;
  399.  
  400.         id3_init(&id3);
  401.         rc = id3_read_tags(&id3, fd, ID3_V1 | ID3_V2);
  402.         if (rc == -1) {
  403.                 d_print("error: %s\n", strerror(errno));
  404.                 goto out;
  405.         }
  406.  
  407.         for (i = 0; i < NUM_ID3_KEYS; i++) {
  408.                 char *val = id3_get_comment(&id3, i);
  409.  
  410.                 if (val)
  411.                         comments_add(&c, id3_key_names[i], val);
  412.         }
  413. out:
  414.         close(fd);
  415.         id3_free(&id3);
  416.         keyvals_terminate(&c);
  417.         *comments = c.keyvals;
  418.         return 0;
  419. }
  420.  
  421. static int aac_duration(struct input_plugin_data *ip_data)
  422. {
  423.         struct aac_private *priv = ip_data->private;
  424.         NeAACDecFrameInfo frame_info;
  425.         int samples = 0, bytes = 0, frames = 0;
  426.         off_t file_size;
  427.  
  428.         file_size = lseek(ip_data->fd, 0, SEEK_END);
  429.         if (file_size == -1)
  430.                 return -IP_ERROR_FUNCTION_NOT_SUPPORTED;
  431.  
  432.         /* Seek to the middle of the file. There is almost always silence at
  433.          * the beginning, which gives wrong results. */
  434.         if (lseek(ip_data->fd, file_size/2, SEEK_SET) == -1)
  435.                 return -IP_ERROR_FUNCTION_NOT_SUPPORTED;
  436.  
  437.         priv->rbuf_pos = 0;
  438.         priv->rbuf_len = 0;
  439.  
  440.         /* guess track length by decoding the first 10 frames */
  441.         while (frames < 10) {
  442.                 if (buffer_fill_frame(ip_data) <= 0)
  443.                         break;
  444.  
  445.                 NeAACDecDecode(priv->decoder, &frame_info,
  446.                         buffer_data(ip_data), buffer_length(ip_data));
  447.                 if (frame_info.error == 0 && frame_info.samples > 0) {
  448.                         samples += frame_info.samples;
  449.                         bytes += frame_info.bytesconsumed;
  450.                         frames++;
  451.                 }
  452.                 if (frame_info.bytesconsumed == 0)
  453.                         break;
  454.  
  455.                 buffer_consume(ip_data, frame_info.bytesconsumed);
  456.         }
  457.  
  458.         if (frames == 0)
  459.                 return -IP_ERROR_FUNCTION_NOT_SUPPORTED;
  460.  
  461.         samples /= frames;
  462.         samples /= priv->channels;
  463.         bytes /= frames;
  464.  
  465.         /*  8 * file_size / duration */
  466.         priv->bitrate = (8 * bytes * priv->sample_rate) / samples;
  467.  
  468.         priv->object_type = frame_info.object_type;
  469.  
  470.         return ((file_size / bytes) * samples) / priv->sample_rate;
  471. }
  472.  
  473. static long aac_bitrate(struct input_plugin_data *ip_data)
  474. {
  475.         struct aac_private *priv = ip_data->private;
  476.         return priv->bitrate != -1 ? priv->bitrate : -IP_ERROR_FUNCTION_NOT_SUPPORTED;
  477. }
  478.  
  479. static long aac_current_bitrate(struct input_plugin_data *ip_data)
  480. {
  481.         struct aac_private *priv = ip_data->private;
  482.         long bitrate = -1;
  483.         if (priv->current.samples > 0) {
  484.                 priv->current.samples /= priv->channels;
  485.                 bitrate = (8 * priv->current.bytes * priv->sample_rate) / priv->current.samples;
  486.                 priv->current.samples = 0;
  487.                 priv->current.bytes = 0;
  488.         }
  489.         return bitrate;
  490. }
  491.  
  492. static char *aac_codec(struct input_plugin_data *ip_data)
  493. {
  494.         return xstrdup("aac");
  495. }
  496.  
  497. static const char *object_type_to_str(int object_type)
  498. {
  499.         switch (object_type) {
  500.         case MAIN:      return "Main";
  501.         case LC:        return "LC";
  502.         case SSR:       return "SSR";
  503.         case LTP:       return "LTP";
  504.         case HE_AAC:    return "HE";
  505.         case ER_LC:     return "ER-LD";
  506.         case ER_LTP:    return "ER-LTP";
  507.         case LD:        return "LD";
  508.         case DRM_ER_LC: return "DRM-ER-LC";
  509.         }
  510.         return NULL;
  511. }
  512.  
  513. static char *aac_codec_profile(struct input_plugin_data *ip_data)
  514. {
  515.         struct aac_private *priv = ip_data->private;
  516.         const char *profile = object_type_to_str(priv->object_type);
  517.  
  518.         return profile ? xstrdup(profile) : NULL;
  519. }
  520.  
  521. const struct input_plugin_ops ip_ops = {
  522.         .open = aac_open,
  523.         .close = aac_close,
  524.         .read = aac_read,
  525.         .seek = aac_seek,
  526.         .read_comments = aac_read_comments,
  527.         .duration = aac_duration,
  528.         .bitrate = aac_bitrate,
  529.         .bitrate_current = aac_current_bitrate,
  530.         .codec = aac_codec,
  531.         .codec_profile = aac_codec_profile
  532. };
  533.  
  534. const int ip_priority = 50;
  535. const char * const ip_extensions[] = { "aac", NULL };
  536. const char * const ip_mime_types[] = { "audio/aac", "audio/aacp", NULL };
  537. const struct input_plugin_opt ip_options[] = { { NULL } };
  538. const unsigned ip_abi_version = IP_ABI_VERSION;
  539.  
downloadaac.c Source code - Download cmus Source code
Related Source Codes/Software:
Ink - An HTML5/CSS3 framework used at SAPO for fast and ... 2017-04-22
memory-stats.js - minimal monitor for JS Heap Size via performance.m... 2017-04-22
LayoutKit - LayoutKit is a fast view layout library for iOS, m... 2017-04-22
Cpp-Primer - C++ Primer 5 answer 2017-04-22
RBBAnimation - Block-based animations made easy, comes with easin... 2017-04-22
phpDocumentor2 - Documentation Generator for PHP ... 2017-04-22
flexboxfroggy - A game for learning CSS flexbox ... 2017-04-22
wicked - Use wicked to turn your controller into a wizard ... 2017-04-22
Begin-Latex-in-minutes - Brief Intro to LaTeX for beginners that helps you ... 2017-04-22
guard-livereload - Guard::LiveReload automatically reload your browse... 2017-04-22
swifter - Tiny http server engine written in Swift programmi... 2017-04-29
Chartbuilder - A front-end charting application that facilitates ... 2017-04-29
offline-first - 2017-04-29
plotly.py - An interactive, browser-based graphing library for... 2017-04-29
statsmodels - Statsmodels: statistical modeling and econometrics... 2017-04-29
android-maps-utils - Handy extensions to the Google Maps Android API. 2017-04-29
enyo - A JavaScript application framework emphasizing mod... 2017-04-29
darkforestGo - DarkForest, the Facebook Go engine. 2017-04-29
erpnext - ERP made Simple http://erpn... 2017-04-29
SwiftInFlux - An attempt to gather all that is in flux in Swift. 2017-04-29

 Back to top