[Libav-user] Open Android mediacodec natively

yoann yoann at woozoom.net
Thu Jan 5 12:02:58 EET 2017


Hi everyone,

I am trying to use FFmpeg's mediacodec wrappers on Android to speed up some h264 decoding.

First if someone has a good exemple of how to use ffmpeg + mediacodec natively I would be pleased to see it !

I have compiled ffmpeg using --enable-decoder=h264_mediacodec --enable-mediacodec --enable-hwaccel=h264_mediacodec and I have trouble using the mediacodec decoder. I detect the h264_mediacodec but cannot open it (return is -542398533, not very explicit). Do I have to set the codec context in some special manner to use HW acceleration ? Also h264 and h264_mediacodec seems to have the same id, how would the decoder know which one to use ?

Regards,
Yoann



int open_stream_component(VideoState *vs, int stream_index){

    AVFormatContext *format_context = vs->format_context;
    AVCodecParameters *codec_parameters = NULL;
    AVCodecContext *codec_context = NULL;
    AVCodec *codec = NULL;
    AVDictionary *options_dict = NULL;
    SDL_AudioSpec wanted_spec, spec;

    codec_parameters = format_context->streams[stream_index]->codecpar;

    AVCodec * cdc = av_codec_next(NULL);
    while(cdc != NULL){
        SDL_Log("%s\n", cdc->long_name);
        cdc = av_codec_next(cdc);
    }

    if(codec_parameters->codec_type == AVMEDIA_TYPE_VIDEO){
      codec = avcodec_find_decoder_by_name("h264_mediacodec");
    } else if(codec_parameters->codec_type == AVMEDIA_TYPE_AUDIO){
      codec = avcodec_find_decoder_by_name("aac");
    }

     SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Codec is %s \n",codec->long_name);

    codec_context = avcodec_alloc_context3(codec);
    avcodec_parameters_to_context(codec_context, codec_parameters);
    codec_context->codec_id = codec->id;

    // Error here
    int ret = 0;
    if((ret = avcodec_open2(codec_context, codec, &options_dict)) < 0) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't open codec, ret = %d", ret);
        return -1;
    }
    
    /* end of code */
}
    
Corresponding Logcat  :
01-05 17:30:04.350: W/SDL/APP(12633): Main thread tid : 12659
01-05 17:30:04.470: V/SDL(12633): onWindowFocusChanged(): true
01-05 17:30:15.610: I/SDL/APP(12633):  ===== Setting input file =====
01-05 17:30:15.610: I/SDL/APP(12633): In set_in_path : in_filepath = /storage/emulated/0/Movies/test.mp4
01-05 17:30:15.610: I/SDL/APP(12633):  ===== Start =====
01-05 17:30:15.610: W/SDL/APP(12633): demux_thread tid : 13218
01-05 17:30:15.700: I/SDL/APP(12633): Network stream open and header read
01-05 17:30:15.800: I/SDL/APP(12633): H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10
01-05 17:30:15.800: I/SDL/APP(12633): H.264 Android MediaCodec decoder
01-05 17:30:15.800: I/SDL/APP(12633): MJPEG (Motion JPEG)
01-05 17:30:15.800: I/SDL/APP(12633): AAC (Advanced Audio Coding)
01-05 17:30:15.800: I/SDL/APP(12633): MP3 (MPEG audio layer 3)
01-05 17:30:15.800: I/SDL/APP(12633): Codec is H.264 Android MediaCodec decoder
01-05 17:30:15.800: E/SDL/APP(12633): Can't open codec, ret = -542398533
01-05 17:30:15.810: I/SDL/APP(12633):  ===== Stop =====



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20170105/364cbaf1/attachment.html>


More information about the Libav-user mailing list