4
votes

I am trying to mux video (H.264) and audio (PCM_S16LE, no compression) into an MPEG transport stream using ffmpeg. The video shows fine. The audio stream, however, does not play. The audio stream, shown by ffprobe is AAC, which is obviously not my intention. So I must be doing something wrong in adding the audio stream. Any idea how I can correct this?

This is my code for adding an audio stream:

void add_audio_stream()
{

    CodecID codec_id = CODEC_ID_PCM_S16LE;

    AVStream *p_ast = av_new_stream(fc, 1);

    if (!p_ast) {
        fprintf(stderr, "Could not alloc audio stream\n");
        exit(1);
    }

    ai = p_ast->index;

    AVCodecContext *pcc = p_ast->codec;
    avcodec_get_context_defaults2( pcc, AVMEDIA_TYPE_AUDIO );

    pcc->codec_type = AVMEDIA_TYPE_AUDIO;
    pcc->codec_id = codec_id;
    pcc->sample_fmt = AV_SAMPLE_FMT_S16;
    //pcc->bit_rate = 44100*16*2;
    pcc->bit_rate = 0;
    pcc->sample_rate = 44100;
    pcc->channels = 2;
    pcc->time_base = (AVRational){1, 44100};


    // some formats want stream headers to be separate
    if (fc->oformat->flags & AVFMT_GLOBALHEADER)
    {
        printf(" **** 1 ****\n");
        pcc->flags |= CODEC_FLAG_GLOBAL_HEADER;
    }
    else
        printf(" **** 2 ****\n");


    AVCodec *codec;

    /* find the audio encoder */
    codec = avcodec_find_encoder(pcc->codec_id);
    if (!codec) {
        fprintf(stderr, "codec not found\n");
        exit(1);
    }


    /* open it */
    if (avcodec_open(pcc, codec) < 0) 
    {
        fprintf(stderr, "could not open codec\n");
        exit(1);
    }
}

Here is the output of ffprobe:

ffprobe version N-32405-g6337de9, Copyright (c) 2007-2011 the FFmpeg developers
  built on Sep  8 2011 11:20:12 with gcc 4.4.3
  configuration: --enable-gpl --enable-version3 --enable-nonfree --enable-postproc --enable-libfaac --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --enable-x11grab --enable-libmp3lame
  libavutil    51. 16. 0 / 51. 16. 0
  libavcodec   53. 13. 0 / 53. 13. 0
  libavformat  53. 12. 0 / 53. 12. 0
  libavdevice  53.  3. 0 / 53.  3. 0
  libavfilter   2. 39. 0 /  2. 39. 0
  libswscale    2.  1. 0 /  2.  1. 0
  libpostproc  51.  2. 0 / 51.  2. 0
[mpegts @ 0xa96daa0] Continuity Check Failed
[mpegts @ 0xa96daa0] Continuity Check Failed
[aac @ 0xa974da0] channel element 0.1 is not allocated
[aac @ 0xa974da0] More than one AAC RDB per ADTS frame is not implemented. Update your FFmpeg version to the newest one from Git. If the problem still occurs, it means that your file has a feature which has not been implemented.

.
.
lot of gobbly-gook about missing AAC parameters . . . 
.
.

[aac @ 0xa974da0] More than one AAC RDB per ADTS frame is not implemented. Update your FFmpeg version to the newest one from Git. If the problem still occurs, it means that your file has a feature which has not been implemented.
[aac @ 0xa974da0] Error decoding AAC frame header.
[mpegts @ 0xa96daa0] max_analyze_duration 5000000 reached at 5429789
[mpegts @ 0xa96daa0] Continuity Check Failed
[mpegts @ 0xa96daa0] Continuity Check Failed

Input #0, mpegts, from 'test_audio_video.mts':
  Duration: 00:00:40.35, start: 0.010000, bitrate: 1907 kb/s
  Program 1 
    Metadata:
      service_name    : Service01
      service_provider: FFmpeg
Stream #0.0[0x100]: Video: h264 (Constrained Baseline) ([27][0][0][0] / 0x001B), yuv420p, 640x480, 30 fps, 30 tbr, 90k tbn, 60 tbc

Stream #0.1[0x101]: Audio: aac ([6][0][0][0] / 0x0006), 96000 Hz, 4.0, s16, 9 kb/s
3

3 Answers

1
votes

I think i doubt that MEPG2 TS will allow PCM Audio. It can take up MPI, MP2 or AAC. AAC is being taken up more as a default choice rather than identificaiton.

Also, unlike Video, Audio headers are not very descriminative. i.e. doesn't have start codes and stuff, so other than PES header, usually there is no way to find out what type of Audio it is.

Encode audio if possible.

Try Gspot application to cross check.

1
votes

Looks like MPEG-TS supports AES3-formatted PCM audio as private data, as specified by SMPTE 302M.

There is currently a s302m encoder/decoder in ffmpeg that will allow you to easily accomplish your goal.

0
votes

some time ago I played with it too. And what I found out is that: - Blu-ray only support 48000 Sample Rate - I always use BIG ENDIAN not LITTLE ENDIAN.

I think ffmpeg will use Blu-ray settings for mpeg2_ts.