Convert line-endings to UNIX for ffmpeg.c

This commit is contained in:
Iwan Timmer
2017-03-18 15:19:42 +01:00
parent d0f9330c90
commit bea030c52b
+183 -183
View File
@@ -1,183 +1,183 @@
/* /*
* This file is part of Moonlight Embedded. * This file is part of Moonlight Embedded.
* *
* Based on Moonlight Pc implementation * Based on Moonlight Pc implementation
* *
* Moonlight is free software; you can redistribute it and/or modify * Moonlight is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or * the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* Moonlight is distributed in the hope that it will be useful, * Moonlight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with Moonlight; if not, see <http://www.gnu.org/licenses/>. * along with Moonlight; if not, see <http://www.gnu.org/licenses/>.
*/ */
#include "ffmpeg.h" #include "ffmpeg.h"
#ifdef HAVE_VDPAU #ifdef HAVE_VDPAU
#include "ffmpeg_vdpau.h" #include "ffmpeg_vdpau.h"
#endif #endif
#include <Limelight.h> #include <Limelight.h>
#include <stdlib.h> #include <stdlib.h>
#include <libswscale/swscale.h> #include <libswscale/swscale.h>
#include <pthread.h> #include <pthread.h>
#include <stdio.h> #include <stdio.h>
#include <stdbool.h> #include <stdbool.h>
// General decoder and renderer state // General decoder and renderer state
static AVPacket pkt; static AVPacket pkt;
static AVCodec* decoder; static AVCodec* decoder;
static AVCodecContext* decoder_ctx; static AVCodecContext* decoder_ctx;
static AVFrame* dec_frame; static AVFrame* dec_frame;
enum decoders {SOFTWARE, VDPAU}; enum decoders {SOFTWARE, VDPAU};
enum decoders decoder_system; enum decoders decoder_system;
#define BYTES_PER_PIXEL 4 #define BYTES_PER_PIXEL 4
// This function must be called before // This function must be called before
// any other decoding functions // any other decoding functions
int ffmpeg_init(int videoFormat, int width, int height, int perf_lvl, int thread_count) { int ffmpeg_init(int videoFormat, int width, int height, int perf_lvl, int thread_count) {
// Initialize the avcodec library and register codecs // Initialize the avcodec library and register codecs
av_log_set_level(AV_LOG_QUIET); av_log_set_level(AV_LOG_QUIET);
avcodec_register_all(); avcodec_register_all();
av_init_packet(&pkt); av_init_packet(&pkt);
#ifdef HAVE_VDPAU #ifdef HAVE_VDPAU
if (perf_lvl & HARDWARE_ACCELERATION) { if (perf_lvl & HARDWARE_ACCELERATION) {
switch (videoFormat) { switch (videoFormat) {
case VIDEO_FORMAT_H264: case VIDEO_FORMAT_H264:
decoder = avcodec_find_decoder_by_name("h264_vdpau"); decoder = avcodec_find_decoder_by_name("h264_vdpau");
break; break;
case VIDEO_FORMAT_H265: case VIDEO_FORMAT_H265:
decoder = avcodec_find_decoder_by_name("hevc_vdpau"); decoder = avcodec_find_decoder_by_name("hevc_vdpau");
break; break;
} }
if (decoder != NULL) if (decoder != NULL)
decoder_system = VDPAU; decoder_system = VDPAU;
} }
#endif #endif
if (decoder == NULL) { if (decoder == NULL) {
decoder_system = SOFTWARE; decoder_system = SOFTWARE;
switch (videoFormat) { switch (videoFormat) {
case VIDEO_FORMAT_H264: case VIDEO_FORMAT_H264:
decoder = avcodec_find_decoder_by_name("h264"); decoder = avcodec_find_decoder_by_name("h264");
break; break;
case VIDEO_FORMAT_H265: case VIDEO_FORMAT_H265:
decoder = avcodec_find_decoder_by_name("hevc"); decoder = avcodec_find_decoder_by_name("hevc");
break; break;
} }
if (decoder == NULL) { if (decoder == NULL) {
printf("Couldn't find decoder\n"); printf("Couldn't find decoder\n");
return -1; return -1;
} }
} }
decoder_ctx = avcodec_alloc_context3(decoder); decoder_ctx = avcodec_alloc_context3(decoder);
if (decoder_ctx == NULL) { if (decoder_ctx == NULL) {
printf("Couldn't allocate context"); printf("Couldn't allocate context");
return -1; return -1;
} }
if (perf_lvl & DISABLE_LOOP_FILTER) if (perf_lvl & DISABLE_LOOP_FILTER)
// Skip the loop filter for performance reasons // Skip the loop filter for performance reasons
decoder_ctx->skip_loop_filter = AVDISCARD_ALL; decoder_ctx->skip_loop_filter = AVDISCARD_ALL;
if (perf_lvl & LOW_LATENCY_DECODE) if (perf_lvl & LOW_LATENCY_DECODE)
// Use low delay single threaded encoding // Use low delay single threaded encoding
decoder_ctx->flags |= CODEC_FLAG_LOW_DELAY; decoder_ctx->flags |= CODEC_FLAG_LOW_DELAY;
if (perf_lvl & SLICE_THREADING) if (perf_lvl & SLICE_THREADING)
decoder_ctx->thread_type = FF_THREAD_SLICE; decoder_ctx->thread_type = FF_THREAD_SLICE;
else else
decoder_ctx->thread_type = FF_THREAD_FRAME; decoder_ctx->thread_type = FF_THREAD_FRAME;
decoder_ctx->thread_count = thread_count; decoder_ctx->thread_count = thread_count;
decoder_ctx->width = width; decoder_ctx->width = width;
decoder_ctx->height = height; decoder_ctx->height = height;
decoder_ctx->pix_fmt = AV_PIX_FMT_YUV420P; decoder_ctx->pix_fmt = AV_PIX_FMT_YUV420P;
int err = avcodec_open2(decoder_ctx, decoder, NULL); int err = avcodec_open2(decoder_ctx, decoder, NULL);
if (err < 0) { if (err < 0) {
printf("Couldn't open codec"); printf("Couldn't open codec");
return err; return err;
} }
dec_frame = av_frame_alloc(); dec_frame = av_frame_alloc();
if (dec_frame == NULL) { if (dec_frame == NULL) {
printf("Couldn't allocate frame"); printf("Couldn't allocate frame");
return -1; return -1;
} }
#ifdef HAVE_VDPAU #ifdef HAVE_VDPAU
if (decoder_system == VDPAU) if (decoder_system == VDPAU)
vdpau_init(decoder_ctx, width, height); vdpau_init(decoder_ctx, width, height);
#endif #endif
return 0; return 0;
} }
// This function must be called after // This function must be called after
// decoding is finished // decoding is finished
void ffmpeg_destroy(void) { void ffmpeg_destroy(void) {
if (decoder_ctx) { if (decoder_ctx) {
avcodec_close(decoder_ctx); avcodec_close(decoder_ctx);
av_free(decoder_ctx); av_free(decoder_ctx);
decoder_ctx = NULL; decoder_ctx = NULL;
} }
if (dec_frame) { if (dec_frame) {
av_frame_free(&dec_frame); av_frame_free(&dec_frame);
dec_frame = NULL; dec_frame = NULL;
} }
} }
AVFrame* ffmpeg_get_frame() { AVFrame* ffmpeg_get_frame() {
if (decoder_system == SOFTWARE) if (decoder_system == SOFTWARE)
return dec_frame; return dec_frame;
#ifdef HAVE_VDPAU #ifdef HAVE_VDPAU
else if (decoder_system == VDPAU) else if (decoder_system == VDPAU)
return vdpau_get_frame(dec_frame); return vdpau_get_frame(dec_frame);
#endif #endif
} }
// packets must be decoded in order // packets must be decoded in order
// indata must be inlen + FF_INPUT_BUFFER_PADDING_SIZE in length // indata must be inlen + FF_INPUT_BUFFER_PADDING_SIZE in length
int ffmpeg_decode(unsigned char* indata, int inlen) { int ffmpeg_decode(unsigned char* indata, int inlen) {
int err; int err;
int got_pic = 0; int got_pic = 0;
pkt.data = indata; pkt.data = indata;
pkt.size = inlen; pkt.size = inlen;
while (pkt.size > 0) { while (pkt.size > 0) {
got_pic = 0; got_pic = 0;
err = avcodec_decode_video2(decoder_ctx, dec_frame, &got_pic, &pkt); err = avcodec_decode_video2(decoder_ctx, dec_frame, &got_pic, &pkt);
if (err < 0) { if (err < 0) {
char errorstring[512]; char errorstring[512];
av_strerror(err, errorstring, sizeof(errorstring)); av_strerror(err, errorstring, sizeof(errorstring));
fprintf(stderr, "Decode failed - %s\n", errorstring); fprintf(stderr, "Decode failed - %s\n", errorstring);
got_pic = 0; got_pic = 0;
break; break;
} }
pkt.size -= err; pkt.size -= err;
pkt.data += err; pkt.data += err;
} }
if (got_pic) { if (got_pic) {
return 1; return 1;
} }
return err < 0 ? err : 0; return err < 0 ? err : 0;
} }