Since the code has been there for a while, I think the best first move would be to update the documentation to at least match the current implementation, i.e.: 'extradata' (when decoding) is owned by the user *except* when using avcodec_free_context; the user should reset the pointer beforehand if they do not want their extradata to be destroyed.

But I agree with jyavenard that a replacement API avcodec_free_context2 that respects the external ownership of extradata would be more consistent with the documentation and other functions.