1
votes

I am passing the output of a MediaExtractor into a MediaCodec decoder, and then passing the decoder's output buffer to an encoder's input buffer. The problem I have is that I need to reduce the resolution from the 1920x1080 output from the decoder to 1280x720 by the time it comes out of the encoder. I can do this using a Surface, but I am trying to target Android 4.1 so will need to achieve this another way. Does anyone know how to change the resolution of a video file using MediaCodec but in a way that is compatible with 4.1?

1
Without Surface input, you'll need to do it in software. MediaCodec is somewhat unstable until 4.3 (API 18). If you use ffmpeg, perhaps through the NDK, you'll have a software-only solution that works on just about any Android device. - fadden
I'm hoping to avoid ffmpeg due to the GPL licensing. From what I've seen it's looking unlikely that I will be able to target <4.3 - user3265561
FFmpeg is LGPL 2.1 or later unless you use a configure option to change the license such as --enable-gpl. - llogan
I need to encode to h264 so I don't think LGPL is an option. One thing I am thinking is possibly rendering from the decoder to a Surface at the target resolution, then feeding each frame from the surface as a bitmap into the encoder. I have no idea yet if that will work though. - user3265561

1 Answers

1
votes

You can use libswscale from libav/ffmpeg, or libyuv, or any other YUV handling library, or write your own downscaling routine - it's not very hard actually.

Basically, when you feed the output from the decoder output buffer into the encoder input buffer, you already can't assume you can do a plain copy, because the two may use different color formats. So to be flexible, your code for copying data already needs to be able to convert any supported decoder output color format into any supported encoder input color format. In this copy step, you can just scale down the data. A trivial nearest neighbor downscale is very simple to implement; better looking scaling require a bit more work.

You don't need to do a full SW decode/encode, you can just use SW to adjust the data in the intermediate copy step. But as fadden pointed out, MediaCodec isn't completely stable prior to 4.3 anyway, so it may still not work on all devices.