aboutsummaryrefslogtreecommitdiff
path: root/example/decode_filter_encode.c
diff options
context:
space:
mode:
Diffstat (limited to 'example/decode_filter_encode.c')
-rw-r--r--example/decode_filter_encode.c364
1 files changed, 364 insertions, 0 deletions
diff --git a/example/decode_filter_encode.c b/example/decode_filter_encode.c
new file mode 100644
index 0000000..48f31a7
--- /dev/null
+++ b/example/decode_filter_encode.c
@@ -0,0 +1,364 @@
+#include <bits/pthreadtypes.h>
+#include <inttypes.h>
+#include <libavcodec/avcodec.h>
+#include <libavfilter/avfilter.h>
+#include <libavformat/avformat.h>
+#include <libavutil/pixdesc.h>
+#include <libswscale/swscale.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <tuberia.h>
+#include <unistd.h>
+
+const char *optstring = "hi:o:s:T";
+
+static void printhelp(const char *argv0)
+{
+ printf(
+ "%s -i <input> -o <output> -s <size> [-T]\n"
+ "%s -h\n"
+ "\n"
+ " -i input the input file\n"
+ " -o output the output file\n"
+ " -s WIDTHxHEIGHT the size to scale the video\n"
+ " -T don't use libtuberia\n"
+ " -h print this help\n", argv0, argv0);
+}
+
+static void close_codec(AVCodecContext **ctx)
+{
+ avcodec_free_context(ctx);
+}
+
+static void close_input(AVFormatContext **ctx)
+{
+ avformat_close_input(ctx);
+}
+
+static int open_input(AVFormatContext **avformatin, AVCodecContext **avcodecin,
+ const char *file)
+{
+ AVFormatContext *ctx = NULL;
+ AVCodec *codec = NULL;
+ int i = 0;
+
+ if (avformatin == NULL || avcodecin == NULL || file == NULL || !file[0]) {
+ return -1;
+ }
+
+ printf("Opening input file %s\n", file);
+ if (avformat_open_input(&ctx, file, NULL, NULL)) {
+ return -1;
+ }
+ printf("Parsing input file %s\n", file);
+ if (avformat_find_stream_info(ctx, NULL) < 0) {
+ close_input(&ctx);
+ return -1;
+ }
+
+ for (i = 0; i < (int)ctx->nb_streams; i++) {
+ if (ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
+ codec = avcodec_find_decoder(ctx->streams[i]->codecpar->codec_id);
+ if (codec == NULL) {
+ fprintf(stderr, "Couldn't find decoder for codec id %d (%s)\n",
+ ctx->streams[i]->codecpar->codec_id,
+ avcodec_get_name(ctx->streams[i]->codecpar->codec_id));
+ close_input(&ctx);
+ return -1;
+ }
+ printf("Opening input codec %s (%d)\n", avcodec_get_name(codec->id),
+ codec->id);
+ *avcodecin = avcodec_alloc_context3(codec);
+ avcodec_parameters_to_context(*avcodecin, ctx->streams[i]->codecpar);
+ (*avcodecin)->time_base = ctx->streams[i]->time_base;
+ if (avcodec_open2(*avcodecin, codec, NULL) < 0) {
+ fprintf(stderr, "Couldn't open codec %d (%s)\n",
+ ctx->streams[i]->codecpar->codec_id,
+ avcodec_get_name(ctx->streams[i]->codecpar->codec_id));
+ close_input(&ctx);
+ close_codec(avcodecin);
+ return -1;
+ }
+ break;
+ }
+ }
+
+ *avformatin = ctx;
+
+ return 0;
+}
+
+static void close_sws(struct SwsContext **ctx)
+{
+ sws_freeContext(*ctx);
+ *ctx = NULL;
+}
+
+static void close_output(AVFormatContext **ctx)
+{
+ av_write_trailer(*ctx);
+ avformat_flush(*ctx);
+ avio_closep(&(*ctx)->pb);
+ avformat_free_context(*ctx);
+ *ctx = NULL;
+}
+
+static int open_output(AVFormatContext **avformatout,
+ AVCodecContext **avcodecout, struct SwsContext **sws, const char *file,
+ const AVFormatContext *input, int width, int height)
+{
+ AVFormatContext *ctx = NULL;
+ int i = 0;
+ const AVCodec *codec = NULL;
+ const AVCodecParameters *codecparin = NULL;
+ AVStream *stream = NULL;
+
+ if (avformatout == NULL || avcodecout == NULL || input == NULL
+ || file == NULL || !file[0]) {
+ return -1;
+ }
+ printf("Opening output file %s\n", file);
+ if (avformat_alloc_output_context2(&ctx, NULL, NULL, file) < 0
+ || ctx == NULL) {
+ return -1;
+ }
+
+ for (i = 0; i < (int)input->nb_streams; i++) {
+ codecparin = input->streams[i]->codecpar;
+ codec = avcodec_find_encoder(codecparin->codec_id);
+ if (codec == NULL) {
+ fprintf(stderr, "Couldn't find encoder for codec id %d (%s)\n",
+ codecparin->codec_id, avcodec_get_name(codecparin->codec_id));
+ close_output(&ctx);
+ return -1;
+ }
+ printf("Opening output codec %s (%d)\n", avcodec_get_name(codec->id),
+ codec->id);
+ stream = avformat_new_stream(ctx, codec);
+ avcodec_parameters_copy(stream->codecpar, codecparin);
+ stream->time_base = input->streams[i]->time_base;
+ if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
+ stream->codecpar->width = width;
+ stream->codecpar->height = height;
+ }
+ if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
+ *avcodecout = avcodec_alloc_context3(codec);
+ avcodec_parameters_to_context(*avcodecout, stream->codecpar);
+ (*avcodecout)->time_base = stream->time_base;
+ if (avcodec_open2(*avcodecout, codec, NULL) < 0) {
+ fprintf(stderr, "Couldn't open codec %s (%d)\n",
+ avcodec_get_name(codecparin->codec_id),
+ codecparin->codec_id);
+ close_output(&ctx);
+ close_codec(avcodecout);
+ return -1;
+ }
+ printf("Opening resize context %dx%d %s -> %dx%d %s\n",
+ codecparin->width, codecparin->height,
+ av_get_pix_fmt_name((enum AVPixelFormat)codecparin->format),
+ stream->codecpar->width, stream->codecpar->height,
+ av_get_pix_fmt_name((enum AVPixelFormat)stream->codecpar->format));
+ *sws = sws_getContext(codecparin->width, codecparin->height,
+ (enum AVPixelFormat)codecparin->format,
+ stream->codecpar->width, stream->codecpar->height,
+ (enum AVPixelFormat)stream->codecpar->format,
+ SWS_GAUSS, NULL, NULL, NULL);
+ if (*sws == NULL) {
+ fprintf(stderr, "Couldn't open resize context %dx%d %s -> %dx%d %s\n",
+ codecparin->width, codecparin->height,
+ av_get_pix_fmt_name((enum AVPixelFormat)codecparin->format),
+ stream->codecpar->width, stream->codecpar->height,
+ av_get_pix_fmt_name((enum AVPixelFormat)stream->codecpar->format));
+ close_output(&ctx);
+ close_codec(avcodecout);
+ close_sws(sws);
+ }
+ }
+ }
+
+ if (!(ctx->flags & AVFMT_NOFILE)) {
+ int ret = avio_open(&ctx->pb, file, AVIO_FLAG_WRITE);
+ if (ret < 0) {
+ fprintf(stderr, "Couldn't create output file %s %s (%d)\n", file,
+ av_err2str(ret), ret);
+ close_output(&ctx);
+ close_codec(avcodecout);
+ close_sws(sws);
+ return -1;
+ }
+ }
+ if (avformat_write_header(ctx, NULL) < 0) {
+ fprintf(stderr, "Couldn't write header to output file %s\n", file);
+ }
+ *avformatout = ctx;
+ return 0;
+}
+
+static void do_tuberia(AVFormatContext *avformatin, AVCodecContext *avcodecin,
+ AVFormatContext *avformatout, struct SwsContext *sws,
+ AVCodecContext *avcodecout)
+{
+ //TODO
+ (void)avformatin;
+ (void)avcodecin;
+ (void)avformatout;
+ (void)avcodecout;
+ (void)sws;
+ printf("Starting to transcode with libtuberia\n");
+ printf("Transcode with libtuberia finished\n");
+}
+
+static int decode_packet(AVCodecContext *ctx, AVPacket *packet, AVFrame *frame)
+{
+ int ret = 0;
+ ret = avcodec_send_packet(ctx, packet);
+ av_packet_unref(packet);
+ if (!ret) {
+ ret = avcodec_receive_frame(ctx, frame);
+ } else {
+ fprintf(stderr, "Couldn't decode video packet\n");
+ }
+
+ return ret;
+}
+
+static int scale_frame(struct SwsContext *sws, AVFrame *frame, AVFrame *scaled,
+ int width, int height, enum AVPixelFormat pix_fmt)
+{
+ av_frame_copy_props(scaled, frame);
+ scaled->width = width;
+ scaled->height = height;
+ scaled->format = pix_fmt;
+ av_frame_get_buffer(scaled, 0);
+ sws_scale(sws, (const uint8_t * const *)frame->data, frame->linesize,
+ 0, frame->height, scaled->data, scaled->linesize);
+ av_frame_unref(frame);
+
+ return 0;
+}
+
+static int encode_frame(AVCodecContext *ctx, AVFrame *frame, AVPacket *packet)
+{
+ int ret = 0;
+
+ ret = avcodec_send_frame(ctx, frame);
+ av_frame_unref(frame);
+ if (!ret) {
+ ret = avcodec_receive_packet(ctx, packet);
+ } else {
+ fprintf(stderr, "Couldn't encode video packet\n");
+ }
+
+ return ret;
+}
+
+static void do_no_tuberia(AVFormatContext *avformatin, AVCodecContext *avcodecin,
+ AVFormatContext *avformatout, struct SwsContext *sws,
+ AVCodecContext *avcodecout)
+{
+ AVPacket *packet = NULL;
+ AVFrame *frame = NULL;
+ AVFrame *scaled = NULL;
+ int idx = 0;
+
+ printf("Starting to transcode without libtuberia\n");
+
+ packet = av_packet_alloc();
+ frame = av_frame_alloc();
+ scaled = av_frame_alloc();
+ while (!av_read_frame(avformatin, packet)) {
+ idx = packet->stream_index;
+ if (avformatin->streams[idx]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
+ if (decode_packet(avcodecin, packet, frame)) {
+ continue;
+ }
+ scale_frame(sws, frame, scaled, avcodecout->width,
+ avcodecout->height, avcodecout->pix_fmt);
+ if (encode_frame(avcodecout, scaled, packet)) {
+ continue;
+ }
+ packet->stream_index = idx;
+ }
+ av_packet_rescale_ts(packet, avformatin->streams[idx]->time_base,
+ avformatout->streams[idx]->time_base);
+ av_interleaved_write_frame(avformatout, packet);
+ }
+ av_packet_free(&packet);
+ av_frame_free(&frame);
+ av_frame_free(&scaled);
+
+ printf("Transcode without libtuberia finished\n");
+}
+
+int main(int argc, char **argv)
+{
+ int c = 0;
+ char *input = NULL;
+ char *output = NULL;
+ int width = 720;
+ int height = 404;
+ int notuberia = 0;
+ AVFormatContext *avformatin = NULL;
+ AVFormatContext *avformatout = NULL;
+ AVCodecContext *avcodecin = NULL;
+ AVCodecContext *avcodecout = NULL;
+ struct SwsContext *sws = NULL;
+
+ while ((c = getopt(argc, argv, optstring)) >= 0) {
+ switch (c) {
+ case 'h':
+ printhelp(argv[0]);
+ return 0;
+ break;
+ case 'i':
+ input = optarg;
+ break;
+ case 'o':
+ output = optarg;
+ break;
+ case 's':
+ if (sscanf(optarg, "%dx%d", &width, &height) != 2) {
+ fprintf(stderr, "Size parameter must be WxH, instead it is %s\n",
+ optarg);
+ return -1;
+ }
+ break;
+ case 'T':
+ notuberia = 1;
+ break;
+ case '?':
+ default:
+ fprintf(stderr, "Incorrect arguments. Run %s -h\n", argv[0]);
+ return -1;
+ }
+ }
+ if (input == NULL || !input[0]) {
+ printhelp(argv[0]);
+ return -1;
+ }
+
+ if (open_input(&avformatin, &avcodecin, input) < 0) {
+ fprintf(stderr, "Couldn't open input %s\n", input);
+ return -1;
+ }
+ if (open_output(&avformatout, &avcodecout, &sws, output, avformatin, width,
+ height) < 0) {
+ fprintf(stderr, "Couldn't initiate output %s\n", output);
+ return -1;
+ }
+
+ if (notuberia) {
+ do_no_tuberia(avformatin, avcodecin, avformatout, sws, avcodecout);
+ } else {
+ do_tuberia(avformatin, avcodecin, avformatout, sws, avcodecout);
+ }
+
+ close_input(&avformatin);
+ close_output(&avformatout);
+ close_codec(&avcodecin);
+ close_codec(&avcodecout);
+ close_sws(&sws);
+
+ return 0;
+}
+