[Libav-user] Open Android mediacodec natively

yoann yoann at woozoom.net
Fri Jan 6 09:50:25 EET 2017


At 2017-01-05 18:02:58, "yoann" <yoann at woozoom.net> wrote:
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



I looked deeper into libavcodec/mediacodec.c public API and without using av_mediacodec_default_init(AVCodecContext *avctx, AVMediaCodecContext *ctx, void *surface) the field AVCodecContext->hwaccel_context is not set. Also this function makes mandatory to provide an android/view/Surface as surface that will be used as an output for the decoder. Then calling av_mediacodec_release_buffer() would bring the decoded data to the android/view/Surface.

But actually I use SDL to do all the screen rendering work (and I need to keep SDL for that), so I just want to get the decoded raw data as an AVFrame after the HW decoding, and have not interest for that android Surface.

Passing an allocated zeroed AVMediaCodecContext structure as hwaccel_context does not work, I still cannot open my h264_mediacodec decoder. If I provide an unused android/view/Surface to that function, will it be possible to later retrieve an AVFrame as an output of avcodec_receive_frame() using HW decoding ?


Also the documentation tells that the field AVCodecContext->hwaccel is set by libavcodec, but when ? Does it needs to be set before opening the codec ?






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/20170106/67a73fe4/attachment.html>


More information about the Libav-user mailing list