diff options
author | Michael Niedermayer <michaelni@gmx.at> | 2012-02-24 02:57:18 +0100 |
---|---|---|
committer | Michael Niedermayer <michaelni@gmx.at> | 2012-02-24 02:57:18 +0100 |
commit | e2cc39b6096ed4353293252e3955417b7766f161 (patch) | |
tree | 0bc4d98c120dedcffb9b6e50943b4fc9e3c2a877 /libavcodec/libxavs.c | |
parent | 32e74395a8e88dee1c149aeb36e7a21df431c181 (diff) | |
parent | 31632e73f47d25e2077fce729571259ee6354854 (diff) | |
download | ffmpeg-e2cc39b6096ed4353293252e3955417b7766f161.tar.gz |
Merge remote-tracking branch 'qatar/master'
* qatar/master: (40 commits)
swf: check return values for av_get/new_packet().
wavpack: Don't shift minclip/maxclip
rtpenc: Expose the max packet size via an avoption
rtpenc: Move max_packet_size to a context variable
rtpenc: Add an option for not sending RTCP packets
lavc: drop encode() support for video.
snowenc: switch to encode2().
snowenc: don't abuse input picture for storing information.
a64multienc: switch to encode2().
a64multienc: don't write into output buffer when there's no output.
libxvid: switch to encode2().
tiffenc: switch to encode2().
tiffenc: properly forward error codes in encode_frame().
lavc: drop libdirac encoder.
gifenc: switch to encode2().
libvpxenc: switch to encode2().
flashsvenc: switch to encode2().
Remove libpostproc.
lcl: don't overwrite input memory.
swscale: take first/lastline over/underflows into account for MMX.
...
Conflicts:
.gitignore
Makefile
cmdutils.c
configure
doc/APIchanges
libavcodec/Makefile
libavcodec/allcodecs.c
libavcodec/libdiracenc.c
libavcodec/libxvidff.c
libavcodec/qtrleenc.c
libavcodec/tiffenc.c
libavcodec/utils.c
libavformat/mov.c
libavformat/movenc.c
libpostproc/Makefile
libpostproc/postprocess.c
libpostproc/postprocess.h
libpostproc/postprocess_altivec_template.c
libpostproc/postprocess_internal.h
libpostproc/postprocess_template.c
libswscale/swscale.c
libswscale/utils.c
Merged-by: Michael Niedermayer <michaelni@gmx.at>
Diffstat (limited to 'libavcodec/libxavs.c')
-rw-r--r-- | libavcodec/libxavs.c | 118 |
1 files changed, 82 insertions, 36 deletions
diff --git a/libavcodec/libxavs.c b/libavcodec/libxavs.c index d7027d1092..0fc69300b5 100644 --- a/libavcodec/libxavs.c +++ b/libavcodec/libxavs.c @@ -37,6 +37,7 @@ #define XAVS_PART_B8X8 0x100 /* Analyze b16x8, b*/ typedef struct XavsContext { + AVClass *class; xavs_param_t params; xavs_t *enc; xavs_picture_t pic; @@ -53,6 +54,9 @@ typedef struct XavsContext { int fast_pskip; int mbtree; int mixed_refs; + + int64_t *pts_buffer; + int out_frame_count; } XavsContext; static void XAVS_log(void *p, int level, const char *fmt, va_list args) @@ -70,13 +74,24 @@ static void XAVS_log(void *p, int level, const char *fmt, va_list args) av_vlog(p, level_map[level], fmt, args); } -static int encode_nals(AVCodecContext *ctx, uint8_t *buf, - int size, xavs_nal_t *nals, - int nnal, int skip_sei) +static int encode_nals(AVCodecContext *ctx, AVPacket *pkt, + xavs_nal_t *nals, int nnal) { XavsContext *x4 = ctx->priv_data; - uint8_t *p = buf; - int i, s; + uint8_t *p; + int i, s, ret, size = x4->sei_size + FF_MIN_BUFFER_SIZE; + + if (!nnal) + return 0; + + for (i = 0; i < nnal; i++) + size += nals[i].i_payload; + + if ((ret = ff_alloc_packet(pkt, size)) < 0) { + av_log(ctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", size); + return ret; + } + p = pkt->data; /* Write the SEI as part of the first frame. */ if (x4->sei_size > 0 && nnal > 0) { @@ -86,30 +101,22 @@ static int encode_nals(AVCodecContext *ctx, uint8_t *buf, } for (i = 0; i < nnal; i++) { - /* Don't put the SEI in extradata. */ - if (skip_sei && nals[i].i_type == NAL_SEI) { - x4->sei = av_malloc( 5 + nals[i].i_payload * 4 / 3 ); - if (xavs_nal_encode(x4->sei, &x4->sei_size, 1, nals + i) < 0) - return -1; - - continue; - } s = xavs_nal_encode(p, &size, 1, nals + i); if (s < 0) return -1; p += s; } + pkt->size = p - pkt->data; - return p - buf; + return 1; } -static int XAVS_frame(AVCodecContext *ctx, uint8_t *buf, - int bufsize, void *data) +static int XAVS_frame(AVCodecContext *ctx, AVPacket *pkt, + const AVFrame *frame, int *got_packet) { XavsContext *x4 = ctx->priv_data; - AVFrame *frame = data; xavs_nal_t *nal; - int nnal, i; + int nnal, i, ret; xavs_picture_t pic_out; x4->pic.img.i_csp = XAVS_CSP_I420; @@ -123,29 +130,44 @@ static int XAVS_frame(AVCodecContext *ctx, uint8_t *buf, x4->pic.i_pts = frame->pts; x4->pic.i_type = XAVS_TYPE_AUTO; + x4->pts_buffer[ctx->frame_number % (ctx->max_b_frames+1)] = frame->pts; } if (xavs_encoder_encode(x4->enc, &nal, &nnal, frame? &x4->pic: NULL, &pic_out) < 0) return -1; - bufsize = encode_nals(ctx, buf, bufsize, nal, nnal, 0); + ret = encode_nals(ctx, pkt, nal, nnal); - if (bufsize < 0) + if (ret < 0) return -1; - if (!bufsize && !frame && !(x4->end_of_stream)){ - buf[bufsize] = 0x0; - buf[bufsize+1] = 0x0; - buf[bufsize+2] = 0x01; - buf[bufsize+3] = 0xb1; - bufsize += 4; - x4->end_of_stream = END_OF_STREAM; - return bufsize; + if (!ret) { + if (!frame && !(x4->end_of_stream)) { + if ((ret = ff_alloc_packet(pkt, 4)) < 0) + return ret; + + pkt->data[0] = 0x0; + pkt->data[1] = 0x0; + pkt->data[2] = 0x01; + pkt->data[3] = 0xb1; + pkt->dts = 2*x4->pts_buffer[(x4->out_frame_count-1)%(ctx->max_b_frames+1)] - + x4->pts_buffer[(x4->out_frame_count-2)%(ctx->max_b_frames+1)]; + x4->end_of_stream = END_OF_STREAM; + *got_packet = 1; + } + return 0; } - /* FIXME: libxavs now provides DTS */ - /* but AVFrame doesn't have a field for it. */ + x4->out_pic.pts = pic_out.i_pts; + pkt->pts = pic_out.i_pts; + if (ctx->has_b_frames) { + if (!x4->out_frame_count) + pkt->dts = pkt->pts - (x4->pts_buffer[1] - x4->pts_buffer[0]); + else + pkt->dts = x4->pts_buffer[(x4->out_frame_count-1)%(ctx->max_b_frames+1)]; + } else + pkt->dts = pkt->pts; switch (pic_out.i_type) { case XAVS_TYPE_IDR: @@ -163,11 +185,16 @@ static int XAVS_frame(AVCodecContext *ctx, uint8_t *buf, /* There is no IDR frame in AVS JiZhun */ /* Sequence header is used as a flag */ - x4->out_pic.key_frame = pic_out.i_type == XAVS_TYPE_I; + if (pic_out.i_type == XAVS_TYPE_I) { + x4->out_pic.key_frame = 1; + pkt->flags |= AV_PKT_FLAG_KEY; + } x4->out_pic.quality = (pic_out.i_qpplus1 - 1) * FF_QP2LAMBDA; - return bufsize; + x4->out_frame_count++; + *got_packet = ret; + return 0; } static av_cold int XAVS_close(AVCodecContext *avctx) @@ -176,6 +203,7 @@ static av_cold int XAVS_close(AVCodecContext *avctx) av_freep(&avctx->extradata); av_free(x4->sei); + av_freep(&x4->pts_buffer); if (x4->enc) xavs_encoder_close(x4->enc); @@ -324,17 +352,35 @@ static av_cold int XAVS_init(AVCodecContext *avctx) if (!x4->enc) return -1; + if (!(x4->pts_buffer = av_mallocz((avctx->max_b_frames+1) * sizeof(*x4->pts_buffer)))) + return AVERROR(ENOMEM); + avctx->coded_frame = &x4->out_pic; /* TAG: Do we have GLOBAL HEADER in AVS */ /* We Have PPS and SPS in AVS */ if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) { xavs_nal_t *nal; - int nnal, s; + int nnal, s, i, size; + uint8_t *p; s = xavs_encoder_headers(x4->enc, &nal, &nnal); - avctx->extradata = av_malloc(s); - avctx->extradata_size = encode_nals(avctx, avctx->extradata, s, nal, nnal, 1); + avctx->extradata = p = av_malloc(s); + for (i = 0; i < nnal; i++) { + /* Don't put the SEI in extradata. */ + if (nal[i].i_type == NAL_SEI) { + x4->sei = av_malloc( 5 + nal[i].i_payload * 4 / 3 ); + if (xavs_nal_encode(x4->sei, &x4->sei_size, 1, nal + i) < 0) + return -1; + + continue; + } + size = xavs_nal_encode(p, &s, 1, nal + i); + if (size < 0) + return -1; + p += size; + } + avctx->extradata_size = p - avctx->extradata; } return 0; } @@ -376,7 +422,7 @@ AVCodec ff_libxavs_encoder = { .id = CODEC_ID_CAVS, .priv_data_size = sizeof(XavsContext), .init = XAVS_init, - .encode = XAVS_frame, + .encode2 = XAVS_frame, .close = XAVS_close, .capabilities = CODEC_CAP_DELAY | CODEC_CAP_AUTO_THREADS, .pix_fmts = (const enum PixelFormat[]) { PIX_FMT_YUV420P, PIX_FMT_NONE }, |