From a97256c1d58fae714ae94301ab09044081d08c8e Mon Sep 17 00:00:00 2001 From: wm4 Date: Sat, 5 Jul 2014 16:45:28 +0200 Subject: demux: move packet functions to a separate source file --- demux/demux.c | 100 -------------------------------------------- demux/demux.h | 8 ---- demux/packet.c | 124 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ demux/packet.h | 9 ++++ old-makefile | 1 + wscript_build.py | 1 + 6 files changed, 135 insertions(+), 108 deletions(-) create mode 100644 demux/packet.c diff --git a/demux/demux.c b/demux/demux.c index f2310d2aa7..c45575f755 100644 --- a/demux/demux.c +++ b/demux/demux.c @@ -29,7 +29,6 @@ #include "config.h" #include "options/options.h" -#include "common/av_common.h" #include "talloc.h" #include "common/msg.h" #include "common/global.h" @@ -41,8 +40,6 @@ #include "audio/format.h" -#include - // Demuxer list extern const struct demuxer_desc demuxer_desc_edl; extern const struct demuxer_desc demuxer_desc_cue; @@ -106,103 +103,6 @@ static void ds_free_packs(struct demux_stream *ds) ds->eof = 0; } -static void packet_destroy(void *ptr) -{ - struct demux_packet *dp = ptr; - talloc_free(dp->avpacket); - av_free(dp->allocation); -} - -static struct demux_packet *create_packet(size_t len) -{ - if (len > 1000000000) { - fprintf(stderr, "Attempt to allocate demux packet over 1 GB!\n"); - abort(); - } - struct demux_packet *dp = talloc(NULL, struct demux_packet); - talloc_set_destructor(dp, packet_destroy); - *dp = (struct demux_packet) { - .len = len, - .pts = MP_NOPTS_VALUE, - .dts = MP_NOPTS_VALUE, - .duration = -1, - .stream_pts = MP_NOPTS_VALUE, - .pos = -1, - .stream = -1, - }; - return dp; -} - -struct demux_packet *new_demux_packet(size_t len) -{ - struct demux_packet *dp = create_packet(len); - dp->buffer = av_malloc(len + FF_INPUT_BUFFER_PADDING_SIZE); - if (!dp->buffer) { - fprintf(stderr, "Memory allocation failure!\n"); - abort(); - } - memset(dp->buffer + len, 0, FF_INPUT_BUFFER_PADDING_SIZE); - dp->allocation = dp->buffer; - return dp; -} - -// data must already have suitable padding, and does not copy the data -struct demux_packet *new_demux_packet_fromdata(void *data, size_t len) -{ - struct demux_packet *dp = create_packet(len); - dp->buffer = data; - return dp; -} - -struct demux_packet *new_demux_packet_from(void *data, size_t len) -{ - struct demux_packet *dp = new_demux_packet(len); - memcpy(dp->buffer, data, len); - return dp; -} - -void demux_packet_shorten(struct demux_packet *dp, size_t len) -{ - assert(len <= dp->len); - dp->len = len; - memset(dp->buffer + dp->len, 0, FF_INPUT_BUFFER_PADDING_SIZE); -} - -void free_demux_packet(struct demux_packet *dp) -{ - talloc_free(dp); -} - -static void destroy_avpacket(void *pkt) -{ - av_free_packet(pkt); -} - -struct demux_packet *demux_copy_packet(struct demux_packet *dp) -{ - struct demux_packet *new = NULL; - if (dp->avpacket) { - assert(dp->buffer == dp->avpacket->data); - assert(dp->len == dp->avpacket->size); - AVPacket *newavp = talloc_zero(NULL, AVPacket); - talloc_set_destructor(newavp, destroy_avpacket); - av_init_packet(newavp); - if (av_packet_ref(newavp, dp->avpacket) < 0) - abort(); - new = new_demux_packet_fromdata(newavp->data, newavp->size); - new->avpacket = newavp; - } - if (!new) { - new = new_demux_packet(dp->len); - memcpy(new->buffer, dp->buffer, new->len); - } - new->pts = dp->pts; - new->dts = dp->dts; - new->duration = dp->duration; - new->stream_pts = dp->stream_pts; - return new; -} - struct sh_stream *new_sh_stream(demuxer_t *demuxer, enum stream_type type) { if (demuxer->num_streams > MAX_SH_STREAMS) { diff --git a/demux/demux.h b/demux/demux.h index fce46faf31..bd963392dd 100644 --- a/demux/demux.h +++ b/demux/demux.h @@ -219,14 +219,6 @@ typedef struct { int aid, vid, sid; //audio, video and subtitle id } demux_program_t; -struct demux_packet *new_demux_packet(size_t len); -// data must already have suitable padding -struct demux_packet *new_demux_packet_fromdata(void *data, size_t len); -struct demux_packet *new_demux_packet_from(void *data, size_t len); -void demux_packet_shorten(struct demux_packet *dp, size_t len); -void free_demux_packet(struct demux_packet *dp); -struct demux_packet *demux_copy_packet(struct demux_packet *dp); - void free_demuxer(struct demuxer *demuxer); int demuxer_add_packet(demuxer_t *demuxer, struct sh_stream *stream, diff --git a/demux/packet.c b/demux/packet.c new file mode 100644 index 0000000000..82de1a6996 --- /dev/null +++ b/demux/packet.c @@ -0,0 +1,124 @@ +/* + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * mpv is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with mpv. If not, see . + */ +#include +#include +#include +#include + +#include + +#include "common/av_common.h" +#include "common/common.h" + +#include "packet.h" + +static void packet_destroy(void *ptr) +{ + struct demux_packet *dp = ptr; + talloc_free(dp->avpacket); + av_free(dp->allocation); +} + +static struct demux_packet *create_packet(size_t len) +{ + if (len > 1000000000) { + fprintf(stderr, "Attempt to allocate demux packet over 1 GB!\n"); + abort(); + } + struct demux_packet *dp = talloc(NULL, struct demux_packet); + talloc_set_destructor(dp, packet_destroy); + *dp = (struct demux_packet) { + .len = len, + .pts = MP_NOPTS_VALUE, + .dts = MP_NOPTS_VALUE, + .duration = -1, + .stream_pts = MP_NOPTS_VALUE, + .pos = -1, + .stream = -1, + }; + return dp; +} + +struct demux_packet *new_demux_packet(size_t len) +{ + struct demux_packet *dp = create_packet(len); + dp->buffer = av_malloc(len + FF_INPUT_BUFFER_PADDING_SIZE); + if (!dp->buffer) { + fprintf(stderr, "Memory allocation failure!\n"); + abort(); + } + memset(dp->buffer + len, 0, FF_INPUT_BUFFER_PADDING_SIZE); + dp->allocation = dp->buffer; + return dp; +} + +// data must already have suitable padding, and does not copy the data +struct demux_packet *new_demux_packet_fromdata(void *data, size_t len) +{ + struct demux_packet *dp = create_packet(len); + dp->buffer = data; + return dp; +} + +struct demux_packet *new_demux_packet_from(void *data, size_t len) +{ + struct demux_packet *dp = new_demux_packet(len); + memcpy(dp->buffer, data, len); + return dp; +} + +void demux_packet_shorten(struct demux_packet *dp, size_t len) +{ + assert(len <= dp->len); + dp->len = len; + memset(dp->buffer + dp->len, 0, FF_INPUT_BUFFER_PADDING_SIZE); +} + +void free_demux_packet(struct demux_packet *dp) +{ + talloc_free(dp); +} + +static void destroy_avpacket(void *pkt) +{ + av_free_packet(pkt); +} + +struct demux_packet *demux_copy_packet(struct demux_packet *dp) +{ + struct demux_packet *new = NULL; + if (dp->avpacket) { + assert(dp->buffer == dp->avpacket->data); + assert(dp->len == dp->avpacket->size); + AVPacket *newavp = talloc_zero(NULL, AVPacket); + talloc_set_destructor(newavp, destroy_avpacket); + av_init_packet(newavp); + if (av_packet_ref(newavp, dp->avpacket) < 0) + abort(); + new = new_demux_packet_fromdata(newavp->data, newavp->size); + new->avpacket = newavp; + } + if (!new) { + new = new_demux_packet(dp->len); + memcpy(new->buffer, dp->buffer, new->len); + } + new->pts = dp->pts; + new->dts = dp->dts; + new->duration = dp->duration; + new->stream_pts = dp->stream_pts; + return new; +} diff --git a/demux/packet.h b/demux/packet.h index ca57179acb..e6cfafb5ee 100644 --- a/demux/packet.h +++ b/demux/packet.h @@ -20,6 +20,7 @@ #define MPLAYER_DEMUX_PACKET_H #include +#include #include // Holds one packet/frame/whatever @@ -38,4 +39,12 @@ typedef struct demux_packet { struct AVPacket *avpacket; // original libavformat packet (demux_lavf) } demux_packet_t; +struct demux_packet *new_demux_packet(size_t len); +// data must already have suitable padding +struct demux_packet *new_demux_packet_fromdata(void *data, size_t len); +struct demux_packet *new_demux_packet_from(void *data, size_t len); +void demux_packet_shorten(struct demux_packet *dp, size_t len); +void free_demux_packet(struct demux_packet *dp); +struct demux_packet *demux_copy_packet(struct demux_packet *dp); + #endif /* MPLAYER_DEMUX_PACKET_H */ diff --git a/old-makefile b/old-makefile index 986c1df828..96e4c48316 100644 --- a/old-makefile +++ b/old-makefile @@ -167,6 +167,7 @@ SOURCES = audio/audio.c \ demux/demux_subreader.c \ demux/ebml.c \ demux/mf.c \ + demux/packet.c \ input/cmd_list.c \ input/cmd_parse.c \ input/event.c \ diff --git a/wscript_build.py b/wscript_build.py index 4dc4ca4b12..1b506d4b1f 100644 --- a/wscript_build.py +++ b/wscript_build.py @@ -184,6 +184,7 @@ def build(ctx): ( "demux/demux_subreader.c" ), ( "demux/ebml.c" ), ( "demux/mf.c" ), + ( "demux/packet.c" ), ## Input ( "input/cmd_list.c" ), -- cgit v1.2.3