Allocate AVPacket for stream->pending

From FFmpeg/doc/APIchanges:

    2021-03-17 - f7db77bd87 - lavc 58.133.100 - codec.h
      Deprecated av_init_packet(). Once removed, sizeof(AVPacket) will
      no longer be a part of the public ABI.

Remove the has_pending boolean, which can be replaced by:

    stream->pending != NULL

Refs #2302 <https://github.com/Genymobile/scrcpy/issues/2302>
This commit is contained in:
Romain Vimont 2021-06-14 09:07:49 +02:00
parent a5d71eee45
commit e8b053ad2f
2 changed files with 26 additions and 21 deletions

View File

@ -104,33 +104,38 @@ static bool
stream_push_packet(struct stream *stream, AVPacket *packet) {
bool is_config = packet->pts == AV_NOPTS_VALUE;
// A config packet must not be decoded immetiately (it contains no
// A config packet must not be decoded immediately (it contains no
// frame); instead, it must be concatenated with the future data packet.
if (stream->has_pending || is_config) {
if (stream->pending || is_config) {
size_t offset;
if (stream->has_pending) {
offset = stream->pending.size;
if (av_grow_packet(&stream->pending, packet->size)) {
if (stream->pending) {
offset = stream->pending->size;
if (av_grow_packet(stream->pending, packet->size)) {
LOGE("Could not grow packet");
return false;
}
} else {
offset = 0;
if (av_new_packet(&stream->pending, packet->size)) {
LOGE("Could not create packet");
stream->pending = av_packet_alloc();
if (!stream->pending) {
LOGE("Could not allocate packet");
return false;
}
if (av_new_packet(stream->pending, packet->size)) {
LOGE("Could not create packet");
av_packet_free(&stream->pending);
return false;
}
stream->has_pending = true;
}
memcpy(stream->pending.data + offset, packet->data, packet->size);
memcpy(stream->pending->data + offset, packet->data, packet->size);
if (!is_config) {
// prepare the concat packet to send to the decoder
stream->pending.pts = packet->pts;
stream->pending.dts = packet->dts;
stream->pending.flags = packet->flags;
packet = &stream->pending;
stream->pending->pts = packet->pts;
stream->pending->dts = packet->dts;
stream->pending->flags = packet->flags;
packet = stream->pending;
}
}
@ -144,10 +149,10 @@ stream_push_packet(struct stream *stream, AVPacket *packet) {
// data packet
bool ok = stream_parse(stream, packet);
if (stream->has_pending) {
if (stream->pending) {
// the pending packet must be discarded (consumed or error)
stream->has_pending = false;
av_packet_unref(&stream->pending);
av_packet_unref(stream->pending);
av_packet_free(&stream->pending);
}
if (!ok) {
@ -233,8 +238,9 @@ run_stream(void *data) {
LOGD("End of frames");
if (stream->has_pending) {
av_packet_unref(&stream->pending);
if (stream->pending) {
av_packet_unref(stream->pending);
av_packet_free(&stream->pending);
}
av_parser_close(stream->parser);
@ -252,7 +258,7 @@ void
stream_init(struct stream *stream, socket_t socket,
const struct stream_callbacks *cbs, void *cbs_userdata) {
stream->socket = socket;
stream->has_pending = false;
stream->pending = NULL;
stream->sink_count = 0;
assert(cbs && cbs->on_eos);

View File

@ -24,8 +24,7 @@ struct stream {
AVCodecParserContext *parser;
// successive packets may need to be concatenated, until a non-config
// packet is available
bool has_pending;
AVPacket pending;
AVPacket *pending;
const struct stream_callbacks *cbs;
void *cbs_userdata;