void DecodeVideo2(const char *outfilename, const char *filename)
{
    /* must be called before using avcodec lib */

    avcodec_register_all();
    FILE* outFile = fopen(outfilename, "wb");
    if (outFile)
    {
        __android_log_write(ANDROID_LOG_DEBUG, "jni_test", "out file exists");
        fclose(outFile);
    }
    __android_log_write(ANDROID_LOG_DEBUG, "jni_test", "Decode video file start.....");
    AVCodec *codec;
    AVCodecContext *c= NULL;
    int frame_count;
    FILE *f;
    AVFrame *frame;
    uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
    AVPacket avpkt;
    av_init_packet(&avpkt);
    /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */
    memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
    ANN_DEBUG

    printf("Decode video file %s to %s\n", filename, outfilename);
    sprintf(msg, "Decode video file %s to %s\n", filename, outfilename);
    __android_log_write(ANDROID_LOG_DEBUG, "jni_test", msg);

    ANN_DEBUG
    /* find the mpeg1 video decoder */
    codec = avcodec_find_decoder(AV_CODEC_ID_H264);
    ANN_DEBUG

    if (!codec) {
        ANN_DEBUG
        sprintf(msg, "Codec not found\n");
        __android_log_write(ANDROID_LOG_DEBUG, "jni_test", msg);
        fprintf(stderr, "Codec not found\n");
        exit(1);
    }
    ANN_DEBUG
    c = avcodec_alloc_context3(codec);
    if (!c) {
        fprintf(stderr, "Could not allocate video codec context\n");
        exit(1);
    }
    ANN_DEBUG
    sprintf(msg, "truncated is removed\n");
    __android_log_write(ANDROID_LOG_DEBUG, "jni_test", msg);
   /* if(codec->capabilities&CODEC_CAP_TRUNCATED)
        c->flags|= CODEC_FLAG_TRUNCATED; */
        /* we do not send complete frames */
    /* For some codecs, such as msmpeg4 and mpeg4, width and height
       MUST be initialized there because this information is not
       available in the bitstream. */
    /* open it */
    ANN_DEBUG
    if (avcodec_open2(c, codec, NULL) < 0) {
        fprintf(stderr, "Could not open codec\n");
        exit(1);
    }
    ANN_DEBUG
    f = fopen(filename, "rb");
    if (!f) {
        fprintf(stderr, "Could not open %s\n", filename);
        exit(1);
    }
    ANN_DEBUG
    frame = av_frame_alloc();
    if (!frame) {
        fprintf(stderr, "Could not allocate video frame\n");
        exit(1);
    }
    ANN_DEBUG

    __android_log_write(ANDROID_LOG_DEBUG, "jni_test", msg);

    for (;;) {
        ANN_DEBUG
        avpkt.size = fread(inbuf, 1, INBUF_SIZE, f);
        sprintf(msg, "avpkt.size = %d, inbuf=0x%x _____ %x\n", avpkt.size, inbuf[0], inbuf[1]);
        if (avpkt.size == 0)
            break;
        __android_log_write(ANDROID_LOG_DEBUG, "jni_test", msg);
        /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)
           and this is the only method to use them because you cannot
           know the compressed data size before analysing it.
           BUT some other codecs (msmpeg4, mpeg4) are inherently frame
           based, so you must call them with all the data for one
           frame exactly. You must also initialize 'width' and
           'height' before initializing them. */
        /* NOTE2: some codecs allow the raw parameters (frame size,
           sample rate) to be changed at any frame. We handle this, so
           you should also take care of it */
        /* here, we use a stream based decoder (mpeg1video), so we
           feed decoder and see if it could decode a frame */
        avpkt.data = inbuf;
        ANN_DEBUG
        while (avpkt.size > 0)
        {
            if (decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 0) < 0)
            {
                sprintf(msg, "fail to write decode frame\n");
                __android_log_write(ANDROID_LOG_DEBUG, "jni_test", msg);
                exit(1);
            }
            sprintf(msg, "%d avpkt.size = %d, frame_count = %d\n", __LINE__, avpkt.size, frame_count);
            __android_log_write(ANDROID_LOG_DEBUG, "jni_test", msg);
        }
    }

    /* some codecs, such as MPEG, transmit the I and P frame with a
       latency of one frame. You must do the following to have a
       chance to get the last frame of the video */
    ANN_DEBUG
    avpkt.data = NULL;
    avpkt.size = 0;


    decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 1);
    ANN_DEBUG
    fclose(f);
    avcodec_close(c);
    av_free(c);
    av_frame_free(&frame);

    printf("\n");

    sprintf(msg, "decode done\n");
    __android_log_write(ANDROID_LOG_DEBUG, "jni_test", msg);

}
Example #2
0
static void video_decode_example(const char *outfilename, const char *filename)
{
    AVCodec *codec;
    AVCodecContext *c= NULL;
    int frame_count;
    FILE *f;
    AVFrame *frame;
    uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
    AVPacket avpkt;

    av_init_packet(&avpkt);

    /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */
    memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);

    printf("Decode video file %s to %s\n", filename, outfilename);

    /* find the mpeg1 video decoder */
    codec = avcodec_find_decoder(AV_CODEC_ID_MPEG1VIDEO);
    if (!codec) {
        fprintf(stderr, "Codec not found\n");
        exit(1);
    }

    c = avcodec_alloc_context3(codec);
    if (!c) {
        fprintf(stderr, "Could not allocate video codec context\n");
        exit(1);
    }

    if(codec->capabilities&CODEC_CAP_TRUNCATED)
        c->flags|= CODEC_FLAG_TRUNCATED; /* we do not send complete frames */

    /* For some codecs, such as msmpeg4 and mpeg4, width and height
       MUST be initialized there because this information is not
       available in the bitstream. */

    /* open it */
    if (avcodec_open2(c, codec, NULL) < 0) {
        fprintf(stderr, "Could not open codec\n");
        exit(1);
    }

    f = fopen(filename, "rb");
    if (!f) {
        fprintf(stderr, "Could not open %s\n", filename);
        exit(1);
    }

    frame = avcodec_alloc_frame();
    if (!frame) {
        fprintf(stderr, "Could not allocate video frame\n");
        exit(1);
    }

    frame_count = 0;
    for(;;) {
        avpkt.size = fread(inbuf, 1, INBUF_SIZE, f);
        if (avpkt.size == 0)
            break;

        /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)
           and this is the only method to use them because you cannot
           know the compressed data size before analysing it.

           BUT some other codecs (msmpeg4, mpeg4) are inherently frame
           based, so you must call them with all the data for one
           frame exactly. You must also initialize 'width' and
           'height' before initializing them. */

        /* NOTE2: some codecs allow the raw parameters (frame size,
           sample rate) to be changed at any frame. We handle this, so
           you should also take care of it */

        /* here, we use a stream based decoder (mpeg1video), so we
           feed decoder and see if it could decode a frame */
        avpkt.data = inbuf;
        while (avpkt.size > 0)
            if (decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 0) < 0)
                exit(1);
    }

    /* some codecs, such as MPEG, transmit the I and P frame with a
       latency of one frame. You must do the following to have a
       chance to get the last frame of the video */
    avpkt.data = NULL;
    avpkt.size = 0;
    decode_write_frame(outfilename, c, frame, &frame_count, &avpkt, 1);

    fclose(f);

    avcodec_close(c);
    av_free(c);
    avcodec_free_frame(&frame);
    printf("\n");
}