fix build issue on platform where fileno is a naive macro
[fdkaac.git] / src / main.c
index 83fb75459c85e1315f33947dc1691f80f4cc36f2..952cfbccc4b91fb92f416992bcdd7326c8b99e6f 100644 (file)
@@ -34,6 +34,7 @@
 #endif
 #include "compat.h"
 #include "wav_reader.h"
+#include "caf_reader.h"
 #include "aacenc.h"
 #include "m4af.h"
 #include "progress.h"
@@ -217,6 +218,8 @@ typedef struct aacenc_param_ex_t {
     const char *raw_format;
 
     aacenc_tag_store_t tags;
+    aacenc_tag_store_t source_tags;
+    aacenc_translate_generic_text_tag_ctx_t source_tag_ctx;
 
     char *json_filename;
 } aacenc_param_ex_t;
@@ -570,11 +573,16 @@ int finalize_m4a(m4af_ctx_t *m4af, const aacenc_param_ex_t *params,
                  HANDLE_AACENCODER encoder)
 {
     unsigned i;
-    aacenc_tag_entry_t *tag = params->tags.tag_table;
+    aacenc_tag_entry_t *tag;
+    
+    tag = params->source_tags.tag_table;
+    for (i = 0; i < params->source_tags.tag_count; ++i, ++tag)
+        aacenc_write_tag_entry(m4af, tag);
 
     if (params->json_filename)
         aacenc_write_tags_from_json(m4af, params->json_filename);
 
+    tag = params->tags.tag_table;
     for (i = 0; i < params->tags.tag_count; ++i, ++tag)
         aacenc_write_tag_entry(m4af, tag);
 
@@ -643,12 +651,15 @@ int parse_raw_spec(const char *spec, pcm_sample_description_t *desc)
     return 0;
 }
 
+static pcm_io_vtbl_t pcm_io_vtbl = {
+    read_callback, seek_callback, tell_callback
+};
+static pcm_io_vtbl_t pcm_io_vtbl_noseek = { read_callback, 0, 0 };
+
 static
 pcm_reader_t *open_input(aacenc_param_ex_t *params)
 {
-    wav_io_context_t wav_io = {
-        read_callback, seek_callback, tell_callback
-    };
+    pcm_io_context_t io = { 0 };
     pcm_reader_t *reader = 0;
     struct stat stb = { 0 };
 
@@ -657,11 +668,13 @@ pcm_reader_t *open_input(aacenc_param_ex_t *params)
                        strerror(errno));
         goto END;
     }
+    io.cookie = params->input_fp;
     if (fstat(fileno(params->input_fp), &stb) == 0
-            && (stb.st_mode & S_IFMT) != S_IFREG) {
-        wav_io.seek = 0;
-        wav_io.tell = 0;
-    }
+            && (stb.st_mode & S_IFMT) == S_IFREG)
+        io.vtbl = &pcm_io_vtbl;
+    else
+        io.vtbl = &pcm_io_vtbl_noseek;
+
     if (params->is_raw) {
         int bytes_per_channel;
         pcm_sample_description_t desc = { 0 };
@@ -673,14 +686,32 @@ pcm_reader_t *open_input(aacenc_param_ex_t *params)
         desc.channels_per_frame = params->raw_channels;
         bytes_per_channel = (desc.bits_per_channel + 7) / 8;
         desc.bytes_per_frame = params->raw_channels * bytes_per_channel;
-        if ((reader = raw_open(&wav_io, params->input_fp, &desc)) == 0) {
+        if ((reader = raw_open(&io, &desc)) == 0) {
             fprintf(stderr, "ERROR: failed to open raw input\n");
             goto END;
         }
     } else {
-        if ((reader = wav_open(&wav_io, params->input_fp,
-                               params->ignore_length)) == 0) {
-            fprintf(stderr, "ERROR: broken / unsupported input file\n");
+        int c;
+        ungetc(c = getc(params->input_fp), params->input_fp);
+
+        switch (c) {
+        case 'R':
+            if ((reader = wav_open(&io, params->ignore_length)) == 0) {
+                fprintf(stderr, "ERROR: broken / unsupported input file\n");
+                goto END;
+            }
+            break;
+        case 'c':
+            params->source_tag_ctx.add = aacenc_add_tag_entry_to_store;
+            params->source_tag_ctx.add_ctx = &params->source_tags;
+            if ((reader = caf_open(&io,
+                                   aacenc_translate_generic_text_tag,
+                                   &params->source_tag_ctx)) == 0) {
+                fprintf(stderr, "ERROR: broken / unsupported input file\n");
+                goto END;
+            }
+            break;
+        default:
             goto END;
         }
     }
@@ -774,7 +805,10 @@ END:
     if (params.output_fp) fclose(params.output_fp);
     if (encoder) aacEncClose(&encoder);
     if (output_filename) free(output_filename);
-    if (params.tags.tag_table) aacenc_free_tag_store(&params.tags);
+    if (params.tags.tag_table)
+        aacenc_free_tag_store(&params.tags);
+    if (params.source_tags.tag_table)
+        aacenc_free_tag_store(&params.source_tags);
 
     return result;
 }
This page took 0.011565 seconds and 4 git commands to generate.