[FFmpeg-devel,1/2] doc/examples/decode_audio: print message about how to play the output file

Submitted by Steven Liu on Aug. 21, 2019, 11:34 a.m.

Details

Message ID 20190821113428.33142-1-lq@chinaffmpeg.org
State New
Headers show

Commit Message

Steven Liu Aug. 21, 2019, 11:34 a.m.
Signed-off-by: Steven Liu <lq@chinaffmpeg.org>
---
 doc/examples/decode_audio.c | 51 +++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)

Comments

lance.lmwang@gmail.com Aug. 23, 2019, 12:29 p.m.
On Wed, Aug 21, 2019 at 07:34:28PM +0800, Steven Liu wrote:
> Signed-off-by: Steven Liu <lq@chinaffmpeg.org>
> ---
>  doc/examples/decode_audio.c | 51 +++++++++++++++++++++++++++++++++++++
>  1 file changed, 51 insertions(+)
> 
> diff --git a/doc/examples/decode_audio.c b/doc/examples/decode_audio.c
> index 19dcafd2c8..23d0c9bf50 100644
> --- a/doc/examples/decode_audio.c
> +++ b/doc/examples/decode_audio.c
> @@ -39,6 +39,35 @@
>  #define AUDIO_INBUF_SIZE 20480
>  #define AUDIO_REFILL_THRESH 4096
>  
> +static int get_format_from_sample_fmt(const char **fmt,
> +                                      enum AVSampleFormat sample_fmt)
> +{
> +    int i;
> +    struct sample_fmt_entry {
> +        enum AVSampleFormat sample_fmt; const char *fmt_be, *fmt_le;
> +    } sample_fmt_entries[] = {
> +        { AV_SAMPLE_FMT_U8,  "u8",    "u8"    },
> +        { AV_SAMPLE_FMT_S16, "s16be", "s16le" },
> +        { AV_SAMPLE_FMT_S32, "s32be", "s32le" },
> +        { AV_SAMPLE_FMT_FLT, "f32be", "f32le" },
> +        { AV_SAMPLE_FMT_DBL, "f64be", "f64le" },
> +    };
> +    *fmt = NULL;
> +
> +    for (i = 0; i < FF_ARRAY_ELEMS(sample_fmt_entries); i++) {
> +        struct sample_fmt_entry *entry = &sample_fmt_entries[i];
> +        if (sample_fmt == entry->sample_fmt) {
> +            *fmt = AV_NE(entry->fmt_be, entry->fmt_le);
> +            return 0;
> +        }
> +    }
> +
> +    fprintf(stderr,
> +            "sample format %s is not supported as output format\n",
> +            av_get_sample_fmt_name(sample_fmt));
> +    return -1;
> +}
> +
>  static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame,
>                     FILE *outfile)
>  {
> @@ -172,6 +201,28 @@ int main(int argc, char **argv)
>      pkt->size = 0;
>      decode(c, pkt, decoded_frame, outfile);
>  
> +    /* print output pcm infomations, because there have no metadata of pcm */
> +    enum AVSampleFormat sfmt = c->sample_fmt;
> +    int n_channels = c->channels;
> +    const char *fmt;
> +
> +    if (av_sample_fmt_is_planar(sfmt)) {
> +        const char *packed = av_get_sample_fmt_name(sfmt);
> +        printf("Warning: the sample format the decoder produced is planar "
> +               "(%s). This example will output the first channel only.\n",
> +               packed ? packed : "?");
> +        sfmt = av_get_packed_sample_fmt(sfmt);
> +    }
> +
> +    n_channels = c->channels;

duplicate code with line#53?

> +    if ((ret = get_format_from_sample_fmt(&fmt, sfmt)) < 0)
> +        goto end;
> +
> +    printf("Play the output audio file with the command:\n"
> +           "ffplay -f %s -ac %d -ar %d %s\n",
> +           fmt, n_channels, c->sample_rate,
> +           outfilename);
> +end:
>      fclose(outfile);
>      fclose(f);
>  
> -- 
> 2.17.2 (Apple Git-113)
> 
> 
> 
> 
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

Patch hide | download patch | download mbox

diff --git a/doc/examples/decode_audio.c b/doc/examples/decode_audio.c
index 19dcafd2c8..23d0c9bf50 100644
--- a/doc/examples/decode_audio.c
+++ b/doc/examples/decode_audio.c
@@ -39,6 +39,35 @@ 
 #define AUDIO_INBUF_SIZE 20480
 #define AUDIO_REFILL_THRESH 4096
 
+static int get_format_from_sample_fmt(const char **fmt,
+                                      enum AVSampleFormat sample_fmt)
+{
+    int i;
+    struct sample_fmt_entry {
+        enum AVSampleFormat sample_fmt; const char *fmt_be, *fmt_le;
+    } sample_fmt_entries[] = {
+        { AV_SAMPLE_FMT_U8,  "u8",    "u8"    },
+        { AV_SAMPLE_FMT_S16, "s16be", "s16le" },
+        { AV_SAMPLE_FMT_S32, "s32be", "s32le" },
+        { AV_SAMPLE_FMT_FLT, "f32be", "f32le" },
+        { AV_SAMPLE_FMT_DBL, "f64be", "f64le" },
+    };
+    *fmt = NULL;
+
+    for (i = 0; i < FF_ARRAY_ELEMS(sample_fmt_entries); i++) {
+        struct sample_fmt_entry *entry = &sample_fmt_entries[i];
+        if (sample_fmt == entry->sample_fmt) {
+            *fmt = AV_NE(entry->fmt_be, entry->fmt_le);
+            return 0;
+        }
+    }
+
+    fprintf(stderr,
+            "sample format %s is not supported as output format\n",
+            av_get_sample_fmt_name(sample_fmt));
+    return -1;
+}
+
 static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame,
                    FILE *outfile)
 {
@@ -172,6 +201,28 @@  int main(int argc, char **argv)
     pkt->size = 0;
     decode(c, pkt, decoded_frame, outfile);
 
+    /* print output pcm infomations, because there have no metadata of pcm */
+    enum AVSampleFormat sfmt = c->sample_fmt;
+    int n_channels = c->channels;
+    const char *fmt;
+
+    if (av_sample_fmt_is_planar(sfmt)) {
+        const char *packed = av_get_sample_fmt_name(sfmt);
+        printf("Warning: the sample format the decoder produced is planar "
+               "(%s). This example will output the first channel only.\n",
+               packed ? packed : "?");
+        sfmt = av_get_packed_sample_fmt(sfmt);
+    }
+
+    n_channels = c->channels;
+    if ((ret = get_format_from_sample_fmt(&fmt, sfmt)) < 0)
+        goto end;
+
+    printf("Play the output audio file with the command:\n"
+           "ffplay -f %s -ac %d -ar %d %s\n",
+           fmt, n_channels, c->sample_rate,
+           outfilename);
+end:
     fclose(outfile);
     fclose(f);