GUAC-236: Provide support for much older versions of libavcodec.

This commit is contained in:
Michael Jumper 2016-03-16 18:48:00 -07:00
parent 1fedb71f76
commit 89f6bd4fae
4 changed files with 158 additions and 35 deletions

View File

@ -51,6 +51,7 @@ guacenc_SOURCES = \
display-layers.c \ display-layers.c \
display-sync.c \ display-sync.c \
encode.c \ encode.c \
ffmpeg-compat.c \
guacenc.c \ guacenc.c \
image-stream.c \ image-stream.c \
instructions.c \ instructions.c \

123
src/guacenc/ffmpeg-compat.c Normal file
View File

@ -0,0 +1,123 @@
/*
* Copyright (C) 2016 Glyptodon, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "config.h"
#include "ffmpeg-compat.h"
#include "log.h"
#include "video.h"
#include <libavcodec/avcodec.h>
#include <libavutil/common.h>
#include <libavutil/imgutils.h>
#include <guacamole/client.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
int guacenc_avcodec_encode_video(guacenc_video* video, AVFrame* frame) {
/* For libavcodec < 54.1.0: avcodec_encode_video2() did not exist */
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54,1,0)
AVCodecContext* context = video->context;
/* Calculate appropriate buffer size */
int length = FF_MIN_BUFFER_SIZE + 12 * context->width * context->height;
/* Allocate space for output */
uint8_t* data = malloc(length);
if (data == NULL)
return -1;
/* Encode packet of video data */
int used = avcodec_encode_video(context, data, length, frame);
if (used < 0) {
guacenc_log(GUAC_LOG_WARNING, "Error encoding frame #%" PRId64,
video->next_pts);
free(data);
return -1;
}
/* Report if no data needs to be written */
if (used == 0) {
free(data);
return 0;
}
/* Write data, logging any errors */
if (fwrite(data, 1, used, video->output) == 0) {
guacenc_log(GUAC_LOG_ERROR, "Unable to write frame "
"#%" PRId64 ": %s", video->next_pts, strerror(errno));
free(data);
return -1;
}
/* Data was written successfully */
free(data);
return 1;
#else
/* Init video packet */
AVPacket packet;
av_init_packet(&packet);
/* Request that encoder allocate data for packet */
packet.data = NULL;
packet.size = 0;
/* Write frame to video */
int got_data;
if (avcodec_encode_video2(video->context, &packet, frame, &got_data) < 0) {
guacenc_log(GUAC_LOG_WARNING, "Error encoding frame #%" PRId64,
video->next_pts);
return -1;
}
/* Write corresponding data to file */
if (got_data) {
/* Write data, logging any errors */
if (fwrite(packet.data, 1, packet.size, video->output) == 0) {
guacenc_log(GUAC_LOG_ERROR, "Unable to write frame "
"#%" PRId64 ": %s", video->next_pts, strerror(errno));
return -1;
}
/* Data was written successfully */
guacenc_log(GUAC_LOG_DEBUG, "Frame #%08" PRId64 ": wrote %i bytes",
video->next_pts, packet.size);
av_packet_unref(&packet);
}
/* Frame may have been queued for later writing / reordering */
else
guacenc_log(GUAC_LOG_DEBUG, "Frame #%08" PRId64 ": queued for later",
video->next_pts);
return got_data;
#endif
}

View File

@ -24,6 +24,7 @@
#define GUACENC_FFMPEG_COMPAT_H #define GUACENC_FFMPEG_COMPAT_H
#include "config.h" #include "config.h"
#include "video.h"
#include <libavcodec/avcodec.h> #include <libavcodec/avcodec.h>
@ -39,6 +40,11 @@
#define av_frame_free avcodec_free_frame #define av_frame_free avcodec_free_frame
#endif #endif
/* For libavcodec < 54.28.0: old avcodec_free_frame() did not exist. */
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54,28,0)
#define avcodec_free_frame av_freep
#endif
/* For libavcodec < 55.52.0: avcodec_free_context did not exist */ /* For libavcodec < 55.52.0: avcodec_free_context did not exist */
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,52,0) #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,52,0)
#define avcodec_free_context av_freep #define avcodec_free_context av_freep
@ -49,5 +55,31 @@
#define av_packet_unref av_free_packet #define av_packet_unref av_free_packet
#endif #endif
/* For libavutil < 51.42.0: AV_PIX_FMT_* was PIX_FMT_* */
#if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(51,42,0)
#define AV_PIX_FMT_RGB32 PIX_FMT_RGB32
#define AV_PIX_FMT_YUV420P PIX_FMT_YUV420P
#endif
/**
* Writes the specied frame as a new frame of video. If pending frames of the
* video are being flushed, the given frame may be NULL (as required by
* avcodec_encode_video2()). If avcodec_encode_video2() does not exist, this
* function will transparently use avcodec_encode_video().
*
* @param video
* The video to write the given frame to.
*
* @param frame
* The frame to write to the video, or NULL if previously-written frames
* are being flushed.
*
* @return
* A positive value if the frame was successfully written, zero if the
* frame has been saved for later writing / reordering, negative if an
* error occurs.
*/
int guacenc_avcodec_encode_video(guacenc_video* video, AVFrame* frame);
#endif #endif

View File

@ -174,47 +174,14 @@ fail_codec:
*/ */
static int guacenc_video_write_frame(guacenc_video* video, AVFrame* frame) { static int guacenc_video_write_frame(guacenc_video* video, AVFrame* frame) {
/* Init video packet */
AVPacket packet;
av_init_packet(&packet);
/* Request that encoder allocate data for packet */
packet.data = NULL;
packet.size = 0;
/* Set timestamp of frame, if frame given */ /* Set timestamp of frame, if frame given */
if (frame != NULL) if (frame != NULL)
frame->pts = video->next_pts; frame->pts = video->next_pts;
/* Write frame to video */ /* Write frame to video */
int got_data; int got_data = guacenc_avcodec_encode_video(video, frame);
if (avcodec_encode_video2(video->context, &packet, frame, &got_data) < 0) { if (got_data < 0)
guacenc_log(GUAC_LOG_WARNING, "Error encoding frame #%" PRId64,
video->next_pts);
return -1; return -1;
}
/* Write corresponding data to file */
if (got_data) {
/* Write data, logging any errors */
if (fwrite(packet.data, 1, packet.size, video->output) == 0) {
guacenc_log(GUAC_LOG_ERROR, "Unable to write frame "
"#%" PRId64 ": %s", video->next_pts, strerror(errno));
return -1;
}
/* Data was written successfully */
guacenc_log(GUAC_LOG_DEBUG, "Frame #%08" PRId64 ": wrote %i bytes",
video->next_pts, packet.size);
av_packet_unref(&packet);
}
/* Frame may have been queued for later writing / reordering */
else
guacenc_log(GUAC_LOG_DEBUG, "Frame #%08" PRId64 ": queued for later",
video->next_pts);
/* Update presentation timestamp for next frame */ /* Update presentation timestamp for next frame */
video->next_pts++; video->next_pts++;