[PATCH] Add a CODEC_CAP_USE_INPUT_BUFFER capabilities flag
Hi all, the new flag will be useful for example in libavfilter when the output frame is accessed *after* the call to av_packet_free(). For example it could be used like this: if (is->video_st->codec->codec->capabilities & CODEC_CAP_USE_INPUT_BUFFER) pkt->destruct = av_destruct_packet_nofree; av_free_packet(pkt); This looks nicer than the use of a codec id check, even in the case of rawdec being the only codec having this capability (I don't know if there are other ones like this). I'm omitting minor version bump in the patch. Regards. -- FFmpeg = Faboulous & Fiendish Merciful Portentous Energized Game
On Oct 5, 2008, at 6:28 PM, Stefano Sabatini wrote:
Hi all,
the new flag will be useful for example in libavfilter when the output frame is accessed *after* the call to av_packet_free().
For example it could be used like this:
if (is->video_st->codec->codec->capabilities & CODEC_CAP_USE_INPUT_BUFFER) pkt->destruct = av_destruct_packet_nofree; av_free_packet(pkt);
This looks nicer than the use of a codec id check, even in the case of rawdec being the only codec having this capability (I don't know if there are other ones like this).
I'm omitting minor version bump in the patch.
I'm not sure of the exact problem details here, but avoiding freeing packets by swapping out the destructor seems fragile to me. As long as AVPackets aren't copied around, using retain counting would work better, especially since we should be using them instead of buf/ buf_size in the avcodec API.
+/** + * Codec will use the input data to store the output frame. You should + * not free the input data if you plan to access later to the output + * frame. + */
I don't understand the first sentence. ...if you plan to access the output frame later...
On date Sunday 2008-10-05 18:44:55 -0400, Alexander Strange encoded:
On Oct 5, 2008, at 6:28 PM, Stefano Sabatini wrote:
Hi all,
the new flag will be useful for example in libavfilter when the output frame is accessed *after* the call to av_packet_free().
For example it could be used like this:
if (is->video_st->codec->codec->capabilities & CODEC_CAP_USE_INPUT_BUFFER) pkt->destruct = av_destruct_packet_nofree; av_free_packet(pkt);
This looks nicer than the use of a codec id check, even in the case of rawdec being the only codec having this capability (I don't know if there are other ones like this).
I'm omitting minor version bump in the patch.
I'm not sure of the exact problem details here, but avoiding freeing packets by swapping out the destructor seems fragile to me. As long as AVPackets aren't copied around, using retain counting would work better, especially since we should be using them instead of buf/ buf_size in the avcodec API.
Do you mean something like: avcodec_decode_videoX(ctx, picture, got_picture_ptr, pkt_ptr); ?
+/** + * Codec will use the input data to store the output frame. You should + * not free the input data if you plan to access later to the output + * frame. + */
I don't understand the first sentence. ...if you plan to access the output frame later...
Updated, attaching the new patch to another mail. Regards. -- FFmpeg = Fantastic & Fast MultiPurpose Exxagerate Gadget
Hi, Stefano Sabatini wrote:
Hi all,
the new flag will be useful for example in libavfilter when the output frame is accessed *after* the call to av_packet_free().
I am not sure I understand the situation, but if libavfilter is accessing a buffer after freeing it, then I guess this is a bug in libavfilter... Why not calling av_packet_free() only after libavfilter finished to access the buffer?
For example it could be used like this:
if (is->video_st->codec->codec->capabilities & CODEC_CAP_USE_INPUT_BUFFER) pkt->destruct = av_destruct_packet_nofree; av_free_packet(pkt);
Maybe I am misunderstanding something, but this looks like a hack: if the destruct method should be av_destruct_packet_nofree(), why not setting it to av_destruct_packet_nofree() since the beginning (in the demuxer), instead of changing it before calling av_free_packet()? I do not know the libavfilter internals, but maybe this problem can be better solved by using some form of reference counting? (when I tried to think about a filter layer some years ago, I arrived to the conclusion that a reference counter for these buffers is needed) Luca
On date Monday 2008-10-06 08:41:51 +0200, Luca Abeni encoded:
Hi,
Stefano Sabatini wrote:
Hi all,
the new flag will be useful for example in libavfilter when the output frame is accessed *after* the call to av_packet_free().
I am not sure I understand the situation, but if libavfilter is accessing a buffer after freeing it, then I guess this is a bug in libavfilter... Why not calling av_packet_free() only after libavfilter finished to access the buffer?
I don't know if this is possible in libavfilter, but consider this situation: two threads working concurrently, the first one extracting packets and decoding them, then the decoded frames are sent to another thread which process them. Since the rawdec decoder uses the same input buffer to set the data buffer in the decoded frames, you can't free the input packet data and *then* access the decoded frame. (BTW I think there are other codecs with this property, I think at least the raw audio decoders, I'll check it better if the general idea I'm pushing will prove to be correct). In this case it's very difficult to free the packet only *after* the output frame has been accessed, since this requires a synchronization between the two threads which should be ideally independent. Making the decoding process aware of the fact that the packet data has not to be freed seems to me like a simpler solution.
For example it could be used like this:
if (is->video_st->codec->codec->capabilities & CODEC_CAP_USE_INPUT_BUFFER) pkt->destruct = av_destruct_packet_nofree; av_free_packet(pkt);
Maybe I am misunderstanding something, but this looks like a hack: if the destruct method should be av_destruct_packet_nofree(), why not setting it to av_destruct_packet_nofree() since the beginning (in the demuxer), instead of changing it before calling av_free_packet()?
Good point, attached patch implement this. I'm not really satisfied with it, indeed in av_read_packet() we don't necessarily already know the stream codec, in that case I'm using avcodec_find_decoder() to find the codec and check for its capabilities, and I'm not completely sure this is a safe solution.
I do not know the libavfilter internals, but maybe this problem can be better solved by using some form of reference counting? (when I tried to think about a filter layer some years ago, I arrived to the conclusion that a reference counter for these buffers is needed)
There is already a reference counting mechanism implemented in libavfilter, but it refers to the already allocated pictures rather than to the input packet buffers. Thanks for the reviews, regards. -- FFmpeg = Frenzy Friendly Minimalistic Portentous Excellent Guru
Hi Stefano, Stefano Sabatini wrote:
On date Monday 2008-10-06 08:41:51 +0200, Luca Abeni encoded:
Hi,
Hi all,
the new flag will be useful for example in libavfilter when the output frame is accessed *after* the call to av_packet_free(). I am not sure I understand the situation, but if libavfilter is accessing a buffer after freeing it, then I guess this is a bug in libavfilter... Why not calling av_packet_free() only after libavfilter finished to access
Stefano Sabatini wrote: the buffer?
I don't know if this is possible in libavfilter
If it's not possible, then I suspect this is a bug: accessing a buffer after having freed it is always a bug. But maybe I am missing some details about libavcodec's internals... :)
but consider this situation: two threads working concurrently, the first one extracting packets and decoding them, then the decoded frames are sent to another thread which process them.
Since the rawdec decoder uses the same input buffer to set the data buffer in the decoded frames, you can't free the input packet data and *then* access the decoded frame. (BTW I think there are other codecs with this property, I think at least the raw audio decoders, I'll check it better if the general idea I'm pushing will prove to be correct).
In this case it's very difficult to free the packet only *after* the output frame has been accessed, since this requires a synchronization between the two threads which should be ideally independent.
If you do not want to synchronise the two threads, the only solution is to duplicate the buffer. Your solution ends up with never freeing the buffer, and I suspect a race between the two threads, with the decoder decoding a frame into a buffer before the second thread can process the previous frame.
Making the decoding process aware of the fact that the packet data has not to be freed seems to me like a simpler solution.
But I do not see anything similar in your patch :) Luca
On date Monday 2008-10-06 23:56:53 +0200, Luca Abeni encoded:
Hi Stefano,
Stefano Sabatini wrote:
On date Monday 2008-10-06 08:41:51 +0200, Luca Abeni encoded:
Hi,
Hi all,
the new flag will be useful for example in libavfilter when the output frame is accessed *after* the call to av_packet_free(). I am not sure I understand the situation, but if libavfilter is accessing a buffer after freeing it, then I guess this is a bug in libavfilter... Why not calling av_packet_free() only after libavfilter finished to access
Stefano Sabatini wrote: the buffer?
I don't know if this is possible in libavfilter
If it's not possible, then I suspect this is a bug: accessing a buffer after having freed it is always a bug. But maybe I am missing some details about libavcodec's internals... :)
but consider this situation: two threads working concurrently, the first one extracting packets and decoding them, then the decoded frames are sent to another thread which process them.
Since the rawdec decoder uses the same input buffer to set the data buffer in the decoded frames, you can't free the input packet data and *then* access the decoded frame. (BTW I think there are other codecs with this property, I think at least the raw audio decoders, I'll check it better if the general idea I'm pushing will prove to be correct).
In this case it's very difficult to free the packet only *after* the output frame has been accessed, since this requires a synchronization between the two threads which should be ideally independent.
If you do not want to synchronise the two threads, the only solution is to duplicate the buffer.
Your solution ends up with never freeing the buffer, and I suspect a race between the two threads, with the decoder decoding a frame into a buffer before the second thread can process the previous frame.
I can't understand here. The packet data buffer will be freed when the output frame will be freed. Also in the two threads scenario the first thread, after the call to av_free_packet (which in this case won't free the input packet data, since that buffer is set in the output frame), simply will pass the output frame to some queue with locked access accessed by the second thread (similar to what happens in ffplay). I can't see how this can lead to some race condition.
Making the decoding process aware of the fact that the packet data has not to be freed seems to me like a simpler solution.
But I do not see anything similar in your patch :)
The packet destruct callback is set to av_destruct_packet_nofree, so the input packet data is not freed. Regards. -- FFmpeg = Foolish & Furious MultiPurpose Ecstatic Gorilla
Hi Stefano, Stefano Sabatini wrote: [...]
Since the rawdec decoder uses the same input buffer to set the data buffer in the decoded frames, you can't free the input packet data and *then* access the decoded frame. (BTW I think there are other codecs with this property, I think at least the raw audio decoders, I'll check it better if the general idea I'm pushing will prove to be correct).
In this case it's very difficult to free the packet only *after* the output frame has been accessed, since this requires a synchronization between the two threads which should be ideally independent. If you do not want to synchronise the two threads, the only solution is to duplicate the buffer.
Your solution ends up with never freeing the buffer, and I suspect a race between the two threads, with the decoder decoding a frame into a buffer before the second thread can process the previous frame.
I can't understand here.
The packet data buffer will be freed when the output frame will be freed.
I might be wrong (I did not double-check the code), but I do not think you need to free the output frame (the decoder will re-use it).
Also in the two threads scenario the first thread, after the call to av_free_packet (which in this case won't free the input packet data, since that buffer is set in the output frame), simply will pass the output frame to some queue with locked access accessed by the second thread (similar to what happens in ffplay).
Note that ffplay calls av_dup_packet() (exactly for this reason, I think). I suspect this function is half-broken, but it should be needed (AFAIK) when passing AVPackets between different threads.
I can't see how this can lead to some race condition.
Sorry, I misunderstood what you meant by "rawdec decoder uses the same input buffer...".
Making the decoding process aware of the fact that the packet data has not to be freed seems to me like a simpler solution. But I do not see anything similar in your patch :)
The packet destruct callback is set to av_destruct_packet_nofree, so the input packet data is not freed.
In this sentence, you were not talking about freeing or not freeing the buffer, you were talking about making the decoding process aware... Anyway, I had a better look at the code, and I suspect the problem is due to a (IMHO) misuse of the API. In my understanding (I hope people will correct me if I am wrong), this is the correct usage pattern: 1) Read a packet 2) Decode the packet 3) Do something with the decoded data 4) Free the packet 5) Do not use the data anymore If you want to do something different (using the decoded data after freeing the packet, using multiple threads, etc...) you need to duplicate the buffer, or to use some similar technique. At least, this is my understanding of how the libav* API should be used (and I think ffmpeg.c does this. ffplay.c uses av_dup_packet()). In any case, I am happily leaving this discussion to people who understand the libav* internal better than me :) Luca
On date Tuesday 2008-10-07 00:44:38 +0200, Luca Abeni encoded:
Hi Stefano,
Stefano Sabatini wrote: [...]
Also in the two threads scenario the first thread, after the call to av_free_packet (which in this case won't free the input packet data, since that buffer is set in the output frame), simply will pass the output frame to some queue with locked access accessed by the second thread (similar to what happens in ffplay).
Note that ffplay calls av_dup_packet() (exactly for this reason, I think). I suspect this function is half-broken, but it should be needed (AFAIK) when passing AVPackets between different threads.
The packets are duped in packet_queue_put() in both ffplay versions, so the data which comes out from the queue is safe, and may be used independently from the source (but need to be freed anyway). [...]
Making the decoding process aware of the fact that the packet data has not to be freed seems to me like a simpler solution. But I do not see anything similar in your patch :)
The packet destruct callback is set to av_destruct_packet_nofree, so the input packet data is not freed.
In this sentence, you were not talking about freeing or not freeing the buffer, you were talking about making the decoding process aware...
Anyway, I had a better look at the code, and I suspect the problem is due to a (IMHO) misuse of the API. In my understanding (I hope people will correct me if I am wrong), this is the correct usage pattern: 1) Read a packet 2) Decode the packet 3) Do something with the decoded data 4) Free the packet 5) Do not use the data anymore
Once we have a duped packet we don't need anymore to worry about the next av_read_frame() read. What I would like to be able to do is to decode the data buffer, eventually free it if it makes sense, discard it and do whatever I want with the output frame. I don't want to know when I have to free the input data when I'm dealing with output packet. In order to achieve this we can use this technique: if we know that the decoder uses the input data to set the output frame, we don't free the input data, other we did it. This way we can avoid memory leaks or worse crashes due to an access to an already freed data. In order to be able to adopt this technique we have to know if the decoder used uses the input buffer to store the output data, otherwise we have to do a specific check on the codec id, which is what I would like to avoid introducing the capability flag. Said that, I read again the libavformat code and got to the conclusion that the second solution which I proposed (implemented in libavformat) was bloody wrong, this is indeed a problem of libavcodec and has more to do with the buffer data than with the use of AVPacket. The following snippet that I used like an example: if (is->video_st->codec->codec->capabilities & CODEC_CAP_USE_INPUT_BUFFER) pkt->destruct = av_destruct_packet_nofree; av_free_packet(pkt); simply makes sure that the data buffer (still used in the output frame) has not to be freed.
If you want to do something different (using the decoded data after freeing the packet, using multiple threads, etc...) you need to duplicate the buffer, or to use some similar technique.
This is already performed in both ffplay and ffplay-libavfilter-soc, since packets are duplicated when they're inserted in the queue.
At least, this is my understanding of how the libav* API should be used (and I think ffmpeg.c does this. ffplay.c uses av_dup_packet()).
In any case, I am happily leaving this discussion to people who understand the libav* internal better than me :)
Your review has been useful anyway, I got forced to read code which I didn't know/remember, at least now I know for sure that the libavformat solution was plain wrong. If the maitainers agree that this feature is needed as I think and the implementation is correct then I'll search for other codecs with the same property. Thanks, regards. -- FFmpeg = Friendly & Freak Mega Proud Earthshaking Game
On Tue, Oct 07, 2008 at 10:29:00PM +0200, Stefano Sabatini wrote:
On date Tuesday 2008-10-07 00:44:38 +0200, Luca Abeni encoded:
Hi Stefano,
Stefano Sabatini wrote: [...]
Also in the two threads scenario the first thread, after the call to av_free_packet (which in this case won't free the input packet data, since that buffer is set in the output frame), simply will pass the output frame to some queue with locked access accessed by the second thread (similar to what happens in ffplay).
Note that ffplay calls av_dup_packet() (exactly for this reason, I think). I suspect this function is half-broken, but it should be needed (AFAIK) when passing AVPackets between different threads.
The packets are duped in packet_queue_put() in both ffplay versions, so the data which comes out from the queue is safe, and may be used independently from the source (but need to be freed anyway).
[...]
Making the decoding process aware of the fact that the packet data has not to be freed seems to me like a simpler solution. But I do not see anything similar in your patch :)
The packet destruct callback is set to av_destruct_packet_nofree, so the input packet data is not freed.
In this sentence, you were not talking about freeing or not freeing the buffer, you were talking about making the decoding process aware...
Anyway, I had a better look at the code, and I suspect the problem is due to a (IMHO) misuse of the API. In my understanding (I hope people will correct me if I am wrong), this is the correct usage pattern: 1) Read a packet 2) Decode the packet 3) Do something with the decoded data 4) Free the packet 5) Do not use the data anymore
Once we have a duped packet we don't need anymore to worry about the next av_read_frame() read.
What I would like to be able to do is to decode the data buffer, eventually free it if it makes sense, discard it and do whatever I want with the output frame.
I don't want to know when I have to free the input data when I'm dealing with output packet.
In order to achieve this we can use this technique: if we know that the decoder uses the input data to set the output frame, we don't free the input data, other we did it.
This way we can avoid memory leaks or worse crashes due to an access to an already freed data.
In order to be able to adopt this technique we have to know if the decoder used uses the input buffer to store the output data, otherwise we have to do a specific check on the codec id, which is what I would like to avoid introducing the capability flag.
Said that, I read again the libavformat code and got to the conclusion that the second solution which I proposed (implemented in libavformat) was bloody wrong, this is indeed a problem of libavcodec and has more to do with the buffer data than with the use of AVPacket.
The following snippet that I used like an example:
if (is->video_st->codec->codec->capabilities & CODEC_CAP_USE_INPUT_BUFFER) pkt->destruct = av_destruct_packet_nofree; av_free_packet(pkt);
simply makes sure that the data buffer (still used in the output frame) has not to be freed.
If you want to do something different (using the decoded data after freeing the packet, using multiple threads, etc...) you need to duplicate the buffer, or to use some similar technique.
This is already performed in both ffplay and ffplay-libavfilter-soc, since packets are duplicated when they're inserted in the queue.
At least, this is my understanding of how the libav* API should be used (and I think ffmpeg.c does this. ffplay.c uses av_dup_packet()).
In any case, I am happily leaving this discussion to people who understand the libav* internal better than me :)
Your review has been useful anyway, I got forced to read code which I didn't know/remember, at least now I know for sure that the libavformat solution was plain wrong.
If the maitainers agree that this feature is needed as I think and the implementation is correct then I'll search for other codecs with the same property.
what you write makes no sense there really are 2 options If your code works (which i doubt) you can just assume every codec uses its input buffer and thus wont need a flagin AVCodec telling you if the codec actually does, not to mention that this can change on a frame by frame basis for codecs mixing raw and compressed data. The second choice is a CODEC_FLAG that disables this use, for this though id like to see at least 2-3 applications that woul actually benefit from this as it adds some extra code into a few decoders and causes a slowdown if the flag is set incorrectly [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB No great genius has ever existed without some touch of madness. -- Aristotle
On date Wednesday 2008-11-26 12:22:36 +0100, Michael Niedermayer encoded:
On Tue, Oct 07, 2008 at 10:29:00PM +0200, Stefano Sabatini wrote:
On date Tuesday 2008-10-07 00:44:38 +0200, Luca Abeni encoded:
Hi Stefano,
Stefano Sabatini wrote: [...]
Also in the two threads scenario the first thread, after the call to av_free_packet (which in this case won't free the input packet data, since that buffer is set in the output frame), simply will pass the output frame to some queue with locked access accessed by the second thread (similar to what happens in ffplay).
Note that ffplay calls av_dup_packet() (exactly for this reason, I think). I suspect this function is half-broken, but it should be needed (AFAIK) when passing AVPackets between different threads.
The packets are duped in packet_queue_put() in both ffplay versions, so the data which comes out from the queue is safe, and may be used independently from the source (but need to be freed anyway).
[...]
Making the decoding process aware of the fact that the packet data has not to be freed seems to me like a simpler solution. But I do not see anything similar in your patch :)
The packet destruct callback is set to av_destruct_packet_nofree, so the input packet data is not freed.
In this sentence, you were not talking about freeing or not freeing the buffer, you were talking about making the decoding process aware...
Anyway, I had a better look at the code, and I suspect the problem is due to a (IMHO) misuse of the API. In my understanding (I hope people will correct me if I am wrong), this is the correct usage pattern: 1) Read a packet 2) Decode the packet 3) Do something with the decoded data 4) Free the packet 5) Do not use the data anymore
Once we have a duped packet we don't need anymore to worry about the next av_read_frame() read.
What I would like to be able to do is to decode the data buffer, eventually free it if it makes sense, discard it and do whatever I want with the output frame.
I don't want to know when I have to free the input data when I'm dealing with output packet.
In order to achieve this we can use this technique: if we know that the decoder uses the input data to set the output frame, we don't free the input data, other we did it.
This way we can avoid memory leaks or worse crashes due to an access to an already freed data.
In order to be able to adopt this technique we have to know if the decoder used uses the input buffer to store the output data, otherwise we have to do a specific check on the codec id, which is what I would like to avoid introducing the capability flag.
Said that, I read again the libavformat code and got to the conclusion that the second solution which I proposed (implemented in libavformat) was bloody wrong, this is indeed a problem of libavcodec and has more to do with the buffer data than with the use of AVPacket.
The following snippet that I used like an example:
if (is->video_st->codec->codec->capabilities & CODEC_CAP_USE_INPUT_BUFFER) pkt->destruct = av_destruct_packet_nofree; av_free_packet(pkt);
simply makes sure that the data buffer (still used in the output frame) has not to be freed.
If you want to do something different (using the decoded data after freeing the packet, using multiple threads, etc...) you need to duplicate the buffer, or to use some similar technique.
This is already performed in both ffplay and ffplay-libavfilter-soc, since packets are duplicated when they're inserted in the queue.
At least, this is my understanding of how the libav* API should be used (and I think ffmpeg.c does this. ffplay.c uses av_dup_packet()).
In any case, I am happily leaving this discussion to people who understand the libav* internal better than me :)
Your review has been useful anyway, I got forced to read code which I didn't know/remember, at least now I know for sure that the libavformat solution was plain wrong.
If the maitainers agree that this feature is needed as I think and the implementation is correct then I'll search for other codecs with the same property.
what you write makes no sense
there really are 2 options If your code works (which i doubt) you can just assume every codec uses its input buffer and thus wont need a flag in AVCodec telling you if the codec actually does, not to mention that this can change on a frame by frame basis for codecs mixing raw and compressed data.
Yes the frame by frame consideration is perfectly valid but I'm not sure about what you mean implementation-level. I'd say we could set a flag in the decoder context telling if the last issued frame used the input buffer. So the check may look like this: if (dec_ctx->used_input_buffer) pkt->destruct = av_destruct_packet_nofree; av_free_packet(pkt);
The second choice is a CODEC_FLAG that disables this use, for this though id like to see at least 2-3 applications that would actually benefit from this as it adds some extra code into a few decoders and causes a slowdown if the flag is set incorrectly
I'm perfectly satisfied with one (or both?) of these solutions, let me know which do you prefer. Regards. -- FFmpeg = Foolish and Free MultiPurpose EnGine
On Sat, Nov 29, 2008 at 09:50:24PM +0100, Stefano Sabatini wrote:
On date Wednesday 2008-11-26 12:22:36 +0100, Michael Niedermayer encoded:
On Tue, Oct 07, 2008 at 10:29:00PM +0200, Stefano Sabatini wrote:
On date Tuesday 2008-10-07 00:44:38 +0200, Luca Abeni encoded:
Hi Stefano,
Stefano Sabatini wrote: [...]
Also in the two threads scenario the first thread, after the call to av_free_packet (which in this case won't free the input packet data, since that buffer is set in the output frame), simply will pass the output frame to some queue with locked access accessed by the second thread (similar to what happens in ffplay).
Note that ffplay calls av_dup_packet() (exactly for this reason, I think). I suspect this function is half-broken, but it should be needed (AFAIK) when passing AVPackets between different threads.
The packets are duped in packet_queue_put() in both ffplay versions, so the data which comes out from the queue is safe, and may be used independently from the source (but need to be freed anyway).
[...]
> Making the decoding process aware of the fact that the packet data has > not to be freed seems to me like a simpler solution. But I do not see anything similar in your patch :)
The packet destruct callback is set to av_destruct_packet_nofree, so the input packet data is not freed.
In this sentence, you were not talking about freeing or not freeing the buffer, you were talking about making the decoding process aware...
Anyway, I had a better look at the code, and I suspect the problem is due to a (IMHO) misuse of the API. In my understanding (I hope people will correct me if I am wrong), this is the correct usage pattern: 1) Read a packet 2) Decode the packet 3) Do something with the decoded data 4) Free the packet 5) Do not use the data anymore
Once we have a duped packet we don't need anymore to worry about the next av_read_frame() read.
What I would like to be able to do is to decode the data buffer, eventually free it if it makes sense, discard it and do whatever I want with the output frame.
I don't want to know when I have to free the input data when I'm dealing with output packet.
In order to achieve this we can use this technique: if we know that the decoder uses the input data to set the output frame, we don't free the input data, other we did it.
This way we can avoid memory leaks or worse crashes due to an access to an already freed data.
In order to be able to adopt this technique we have to know if the decoder used uses the input buffer to store the output data, otherwise we have to do a specific check on the codec id, which is what I would like to avoid introducing the capability flag.
Said that, I read again the libavformat code and got to the conclusion that the second solution which I proposed (implemented in libavformat) was bloody wrong, this is indeed a problem of libavcodec and has more to do with the buffer data than with the use of AVPacket.
The following snippet that I used like an example:
if (is->video_st->codec->codec->capabilities & CODEC_CAP_USE_INPUT_BUFFER) pkt->destruct = av_destruct_packet_nofree; av_free_packet(pkt);
simply makes sure that the data buffer (still used in the output frame) has not to be freed.
If you want to do something different (using the decoded data after freeing the packet, using multiple threads, etc...) you need to duplicate the buffer, or to use some similar technique.
This is already performed in both ffplay and ffplay-libavfilter-soc, since packets are duplicated when they're inserted in the queue.
At least, this is my understanding of how the libav* API should be used (and I think ffmpeg.c does this. ffplay.c uses av_dup_packet()).
In any case, I am happily leaving this discussion to people who understand the libav* internal better than me :)
Your review has been useful anyway, I got forced to read code which I didn't know/remember, at least now I know for sure that the libavformat solution was plain wrong.
If the maitainers agree that this feature is needed as I think and the implementation is correct then I'll search for other codecs with the same property.
what you write makes no sense
there really are 2 options If your code works (which i doubt) you can just assume every codec uses its input buffer and thus wont need a flag in AVCodec telling you if the codec actually does, not to mention that this can change on a frame by frame basis for codecs mixing raw and compressed data.
Yes the frame by frame consideration is perfectly valid
but I'm not sure about what you mean implementation-level.
i mean the case identical to yours, just that all codecs always set the flag even if its not correct. What would break? The problem simply is that i dont see where you ever free the packet when the flag is set. It simply leaks ...
I'd say we could set a flag in the decoder context telling if the last issued frame used the input buffer.
So the check may look like this:
if (dec_ctx->used_input_buffer) pkt->destruct = av_destruct_packet_nofree; av_free_packet(pkt);
and where is the buffer then freed? your code is equivalent to if (!dec_ctx->used_input_buffer) av_free_packet(pkt); [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Concerning the gods, I have no means of knowing whether they exist or not or of what sort they may be, because of the obscurity of the subject, and the brevity of human life -- Protagoras
On date Sunday 2008-11-30 17:53:47 +0100, Michael Niedermayer encoded:
On Sat, Nov 29, 2008 at 09:50:24PM +0100, Stefano Sabatini wrote:
On date Wednesday 2008-11-26 12:22:36 +0100, Michael Niedermayer encoded: [...]
there really are 2 options If your code works (which i doubt) you can just assume every codec uses its input buffer and thus wont need a flag in AVCodec telling you if the codec actually does, not to mention that this can change on a frame by frame basis for codecs mixing raw and compressed data.
Yes the frame by frame consideration is perfectly valid
but I'm not sure about what you mean implementation-level.
i mean the case identical to yours, just that all codecs always set the flag even if its not correct. What would break?
The problem simply is that i dont see where you ever free the packet when the flag is set. It simply leaks ...
I was thinking that there was some code which explicitely freed the decoded frame, which is simply not true, as the decoder seem to store the decoded frame in their internal buffer (as far as I (mis)understood it). Then my solution was generating a memleak, since in that case the frame data wasn't never freed, neither by the decoder neiter by some application code. So the only way to fix the problem in libavfilter-soc/ff{play,mpeg).c seems to me to posticipate the freeing of the packet after the decoded frame has been copied in the AVFilterPicRef struct. I'll try to give it a try, if the resulting code would result too ugly/messy then I'll consider your second proposal:
The second choice is a CODEC_FLAG that disables this use, for this though id like to see at least 2-3 applications that would actually benefit from this as it adds some extra code into a few decoders and causes a slowdown if the flag is set incorrectly
Thanks, regards. -- FFmpeg = Frightening and Fundamental MultiPurpose Egregious Gadget
On date Sunday 2008-11-30 19:58:16 +0100, Stefano Sabatini encoded:
On date Sunday 2008-11-30 17:53:47 +0100, Michael Niedermayer encoded:
On Sat, Nov 29, 2008 at 09:50:24PM +0100, Stefano Sabatini wrote:
On date Wednesday 2008-11-26 12:22:36 +0100, Michael Niedermayer encoded: [...]
there really are 2 options If your code works (which i doubt) you can just assume every codec uses its input buffer and thus wont need a flag in AVCodec telling you if the codec actually does, not to mention that this can change on a frame by frame basis for codecs mixing raw and compressed data.
Yes the frame by frame consideration is perfectly valid
but I'm not sure about what you mean implementation-level.
i mean the case identical to yours, just that all codecs always set the flag even if its not correct. What would break?
The problem simply is that i dont see where you ever free the packet when the flag is set. It simply leaks ...
I was thinking that there was some code which explicitely freed the decoded frame, which is simply not true, as the decoder seem to store the decoded frame in their internal buffer (as far as I (mis)understood it).
Then my solution was generating a memleak, since in that case the frame data wasn't never freed, neither by the decoder neiter by some application code.
So the only way to fix the problem in libavfilter-soc/ff{play,mpeg).c seems to me to posticipate the freeing of the packet after the decoded frame has been copied in the AVFilterPicRef struct.
The attached patch fixes the memleak (which I introduced fixing a crash) occurring when a raw video decoder is used. It does so redefining the function get_video_frame(), which won't immediately free the data in the packet (which could be used for example by the raw decoder to contain the frame data), but returns a shallow copy to the packet just extracted from the input packet queue. The packet is then freed just after the use of the corresponding frame. [...] Regards. -- FFmpeg = Freak & Foolish Meaningless Plastic EnGraver
Stefano Sabatini wrote:
On date Sunday 2008-11-30 19:58:16 +0100, Stefano Sabatini encoded:
On Sat, Nov 29, 2008 at 09:50:24PM +0100, Stefano Sabatini wrote:
On date Wednesday 2008-11-26 12:22:36 +0100, Michael Niedermayer encoded: [...]
there really are 2 options If your code works (which i doubt) you can just assume every codec uses its input buffer and thus wont need a flag in AVCodec telling you if the codec actually does, not to mention that this can change on a frame by frame basis for codecs mixing raw and compressed data. Yes the frame by frame consideration is perfectly valid but I'm not sure about what you mean implementation-level. i mean the case identical to yours, just that all codecs always set the flag even if its not correct. What would break?
The problem simply is that i dont see where you ever free the packet when the flag is set. It simply leaks ... I was thinking that there was some code which explicitely freed the decoded frame, which is simply not true, as the decoder seem to store
On date Sunday 2008-11-30 17:53:47 +0100, Michael Niedermayer encoded: the decoded frame in their internal buffer (as far as I (mis)understood it).
Then my solution was generating a memleak, since in that case the frame data wasn't never freed, neither by the decoder neiter by some application code.
So the only way to fix the problem in libavfilter-soc/ff{play,mpeg).c seems to me to posticipate the freeing of the packet after the decoded frame has been copied in the AVFilterPicRef struct.
The attached patch fixes the memleak (which I introduced fixing a crash) occurring when a raw video decoder is used.
It does so redefining the function get_video_frame(), which won't immediately free the data in the packet (which could be used for example by the raw decoder to contain the frame data), but returns a shallow copy to the packet just extracted from the input packet queue.
The packet is then freed just after the use of the corresponding frame.
Looks fine for me. -Vitor
On date Friday 2008-12-05 18:34:17 +0100, Vitor Sessak encoded:
Stefano Sabatini wrote:
On date Sunday 2008-11-30 19:58:16 +0100, Stefano Sabatini encoded:
On Sat, Nov 29, 2008 at 09:50:24PM +0100, Stefano Sabatini wrote:
On date Wednesday 2008-11-26 12:22:36 +0100, Michael Niedermayer encoded: [...]
there really are 2 options If your code works (which i doubt) you can just assume every codec uses its input buffer and thus wont need a flag in AVCodec telling you if the codec actually does, not to mention that this can change on a frame by frame basis for codecs mixing raw and compressed data. Yes the frame by frame consideration is perfectly valid but I'm not sure about what you mean implementation-level. i mean the case identical to yours, just that all codecs always set the flag even if its not correct. What would break?
The problem simply is that i dont see where you ever free the packet when the flag is set. It simply leaks ... I was thinking that there was some code which explicitely freed the decoded frame, which is simply not true, as the decoder seem to store
On date Sunday 2008-11-30 17:53:47 +0100, Michael Niedermayer encoded: the decoded frame in their internal buffer (as far as I (mis)understood it).
Then my solution was generating a memleak, since in that case the frame data wasn't never freed, neither by the decoder neiter by some application code.
So the only way to fix the problem in libavfilter-soc/ff{play,mpeg).c seems to me to posticipate the freeing of the packet after the decoded frame has been copied in the AVFilterPicRef struct.
The attached patch fixes the memleak (which I introduced fixing a crash) occurring when a raw video decoder is used.
It does so redefining the function get_video_frame(), which won't immediately free the data in the packet (which could be used for example by the raw decoder to contain the frame data), but returns a shallow copy to the packet just extracted from the input packet queue.
The packet is then freed just after the use of the corresponding frame.
Looks fine for me.
Applied, regards. -- FFmpeg = Fiendish & Foolish Martial Portable Erroneous God
participants (5)
-
astrange@ithinksw.com -
lucabe72@email.it -
michaelni@gmx.at -
stefano.sabatini-lala@poste.it -
vitor1001@gmail.com