2 * Copyright (c) 2008 Luca Abeni
4 * This is free software; see GPL.txt
6 /*#include "libavformat/avformat.h"
7 #include "libavcodec/avcodec.h"
8 #include "libswscale/swscale.h"*/
10 #include <libavformat/avformat.h>
11 #include <libavcodec/avcodec.h>
12 #include <libswscale/swscale.h>
14 struct resample_data {
15 struct SwsContext *resample_ctx;
17 AVFrame resampled_frame;
20 int codec_open(AVFormatContext *ctx)
25 for (i = 0; i < ctx->nb_streams; i++) {
26 codec = avcodec_find_decoder(ctx->streams[i]->codec->codec_id);
28 fprintf(stderr, "Cannot find codec %d for input stream %d\n",
29 ctx->streams[i]->codec->codec_id, i);
32 res = avcodec_open(ctx->streams[i]->codec, codec);
34 fprintf(stderr, "Cannot open codec for input stream %d\n", i);
43 int out_codec_open(AVFormatContext *ctx)
48 for (i = 0; i < ctx->nb_streams; i++) {
49 codec = avcodec_find_encoder(ctx->streams[i]->codec->codec_id);
51 fprintf(stderr, "Cannot find codec %d for output stream %d\n",
52 ctx->streams[i]->codec->codec_id, i);
55 res = avcodec_open(ctx->streams[i]->codec, codec);
57 fprintf(stderr, "Cannot open codec for input stream %d\n", i);
67 AVFrame *pkt_decode(AVFormatContext *ctx, AVPacket *pkt)
72 avcodec_get_frame_defaults(&frame);
73 res = avcodec_decode_video(ctx->streams[pkt->stream_index]->codec,
74 &frame, &got, pkt->data, pkt->size);
81 struct resample_data *r;
83 r = ctx->streams[pkt->stream_index]->codec->opaque;
85 sws_scale(r->resample_ctx, frame.data, frame.linesize, 0,
86 ctx->streams[pkt->stream_index]->codec->height,
87 r->resampled_frame.data, r->resampled_frame.linesize);
88 res = &r->resampled_frame;
99 int codec_connect(AVCodecContext *ic, AVCodecContext *oc)
101 struct resample_data *r;
103 if ((ic->width == oc->width) && (ic->height == oc->height) && (ic->pix_fmt == oc->pix_fmt)) {
107 r = av_malloc(sizeof(struct resample_data));
108 r->resample_ctx = sws_getContext(ic->width, ic->height, ic->pix_fmt,
109 oc->width, oc->height, oc->pix_fmt,
110 SWS_BICUBIC, NULL, NULL, NULL);
111 if (r->resample_ctx == NULL) {
116 avcodec_get_frame_defaults(&r->resampled_frame);
117 avpicture_alloc((AVPicture*)&r->resampled_frame,
118 oc->pix_fmt, oc->width, oc->height);
124 AVPacket *pkt_encode(AVFormatContext *ctx, AVFrame *frame)
126 AVCodecContext *c = ctx->streams[0]->codec;
130 pkt.size = 256 * 1024;
131 if (pkt.data == NULL) {
132 pkt.data = av_malloc(pkt.size);
134 res = avcodec_encode_video(c, pkt.data, pkt.size, frame);
135 if (c->coded_frame->pts != AV_NOPTS_VALUE) {
136 pkt.pts = av_rescale_q(c->coded_frame->pts, c->time_base, ctx->streams[0]->time_base);