History

Revert "avcodec: add FF_CODEC_CAP_INIT_CLEANUP for all codecs which use ff_mpv_common_init()"

This mostly reverts commit 4b2863ff01b1fe93d9a518523c9098d17a9d8c6f. Said commit removed the freeing code from ff_mpv_common_init(), ff_mpv_common_frame_size_change() and ff_mpeg_framesize_alloc() and instead added the FF_CODEC_CAP_INIT_CLEANUP to several codecs that use ff_mpv_common_init(). This introduced several bugs: a) Several decoders using ff_mpv_common_init() in their init function were forgotten: This affected FLV, Intel H.263, RealVideo 3.0 and V4.0 as well as VC-1/WMV3. b) ff_mpv_common_init() is not only called from the init function of codecs, it is also called from AVCodec.decode functions. If an error happens after an allocation has succeeded, it can lead to memleaks; furthermore, it is now possible for the MpegEncContext to be marked as initialized even when ff_mpv_common_init() returns an error and this can lead to segfaults because decoders that call ff_mpv_common_init() when decoding a frame can mistakenly think that the MpegEncContext has been properly initialized. This can e.g. happen with H.261 or MPEG-4. c) Removing code for freeing from ff_mpeg_framesize_alloc() (which can't be called from any init function) can lead to segfaults because the check for whether it needs to allocate consists of checking whether the first of the buffers allocated there has been allocated. This part has already been fixed in 76cea1d2ce3f23e8131c8664086a1daf873ed694. d) ff_mpv_common_frame_size_change() can also not be reached from any AVCodec.init function; yet the changes can e.g. lead to segfaults with decoders using ff_h263_decode_frame() upon allocation failure, because the MpegEncContext will upon return be flagged as both initialized and not in need of reinitialization (granted, the fact that ff_h263_decode_frame() clears context_reinit before the context has been reinited is a bug in itself). With the earlier version, the context would be cleaned upon failure and it would be attempted to initialize the context again in the next call to ff_h263_decode_frame(). While a) could be fixed by adding the missing FF_CODEC_CAP_INIT_CLEANUP, keeping the current approach would entail adding cleanup code to several other places because of b). Therefore ff_mpv_common_init() is again made to clean up after itself; the changes to the wmv2 decoder and the SVQ1 encoder have not been reverted: The former fixed a memleak, the latter allowed to remove cleanup code. Fixes: double free Fixes: ff_free_picture_tables.mp4 Fixes: ff_mpeg_update_thread_context.mp4 Fixes: decode_colskip.mp4 Fixes: memset.mp4

Andreas Rheinhardt committed 5 years ago

Commit d4b9e111 parent fb4da907 files · +34 −27Browse files

Unified · Split

7 files
  1. libavcodec/h261dec.c +1 −1
  2. libavcodec/h263dec.c +2 −2
  3. libavcodec/mpeg12dec.c +5 −4
  4. libavcodec/mpeg4videodec.c +1 −2
  5. libavcodec/mpegvideo.c +21 −12
  6. libavcodec/msmpeg4dec.c +4 −4
  7. libavcodec/rv10.c +0 −2
libavcodec/h261dec.c+1−1
@@ -678,6 +678,6 @@ AVCodec ff_h261_decoder = { · 677 unmodified lines
678 .close = h261_decode_end,678 .close = h261_decode_end,
679 .decode = h261_decode_frame,679 .decode = h261_decode_frame,
680 .capabilities = AV_CODEC_CAP_DR1,680 .capabilities = AV_CODEC_CAP_DR1,
681 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,681 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
682 .max_lowres = 3,682 .max_lowres = 3,
683};683};
libavcodec/h263dec.c+2−2
@@ -771,7 +771,7 @@ AVCodec ff_h263_decoder = { · 770 unmodified lines
771 .decode = ff_h263_decode_frame,771 .decode = ff_h263_decode_frame,
772 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |772 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
773 AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY,773 AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY,
774 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM | FF_CODEC_CAP_INIT_CLEANUP,774 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
775 .flush = ff_mpeg_flush,775 .flush = ff_mpeg_flush,
776 .max_lowres = 3,776 .max_lowres = 3,
777 .pix_fmts = ff_h263_hwaccel_pixfmt_list_420,777 .pix_fmts = ff_h263_hwaccel_pixfmt_list_420,
@@ -789,7 +789,7 @@ AVCodec ff_h263p_decoder = { · 11 unmodified lines
789 .decode = ff_h263_decode_frame,789 .decode = ff_h263_decode_frame,
790 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |790 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
791 AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY,791 AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY,
792 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM | FF_CODEC_CAP_INIT_CLEANUP,792 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
793 .flush = ff_mpeg_flush,793 .flush = ff_mpeg_flush,
794 .max_lowres = 3,794 .max_lowres = 3,
795 .pix_fmts = ff_h263_hwaccel_pixfmt_list_420,795 .pix_fmts = ff_h263_hwaccel_pixfmt_list_420,
libavcodec/mpeg12dec.c+5−4
@@ -2880,7 +2880,8 @@ static av_cold int mpeg_decode_end(AVCodecContext *avctx) · 2879 unmodified lines
2880{2880{
2881 Mpeg1Context *s = avctx->priv_data;2881 Mpeg1Context *s = avctx->priv_data;
2882 2882
2883 ff_mpv_common_end(&s->mpeg_enc_ctx);2883 if (s->mpeg_enc_ctx_allocated)
2884 ff_mpv_common_end(&s->mpeg_enc_ctx);
2884 av_buffer_unref(&s->a53_buf_ref);2885 av_buffer_unref(&s->a53_buf_ref);
2885 return 0;2886 return 0;
2886}2887}
@@ -2897,7 +2898,7 @@ AVCodec ff_mpeg1video_decoder = { · 10 unmodified lines
2897 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |2898 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
2898 AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY |2899 AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY |
2899 AV_CODEC_CAP_SLICE_THREADS,2900 AV_CODEC_CAP_SLICE_THREADS,
2900 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP |2901 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
2901 FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,2902 FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
2902 .flush = flush,2903 .flush = flush,
2903 .max_lowres = 3,2904 .max_lowres = 3,
@@ -2931,7 +2932,7 @@ AVCodec ff_mpeg2video_decoder = { · 27 unmodified lines
2931 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |2932 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
2932 AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY |2933 AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY |
2933 AV_CODEC_CAP_SLICE_THREADS,2934 AV_CODEC_CAP_SLICE_THREADS,
2934 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP |2935 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
2935 FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,2936 FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
2936 .flush = flush,2937 .flush = flush,
2937 .max_lowres = 3,2938 .max_lowres = 3,
@@ -2976,7 +2977,7 @@ AVCodec ff_mpegvideo_decoder = { · 38 unmodified lines
2976 .close = mpeg_decode_end,2977 .close = mpeg_decode_end,
2977 .decode = mpeg_decode_frame,2978 .decode = mpeg_decode_frame,
2978 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 | AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS,2979 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 | AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS,
2979 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP |2980 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
2980 FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,2981 FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
2981 .flush = flush,2982 .flush = flush,
2982 .max_lowres = 3,2983 .max_lowres = 3,
libavcodec/mpeg4videodec.c+1−2
@@ -3579,8 +3579,7 @@ AVCodec ff_mpeg4_decoder = { · 3578 unmodified lines
3579 AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY |3579 AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY |
3580 AV_CODEC_CAP_FRAME_THREADS,3580 AV_CODEC_CAP_FRAME_THREADS,
3581 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM |3581 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM |
3582 FF_CODEC_CAP_ALLOCATE_PROGRESS |3582 FF_CODEC_CAP_ALLOCATE_PROGRESS,
3583 FF_CODEC_CAP_INIT_CLEANUP,
3584 .flush = ff_mpeg_flush,3583 .flush = ff_mpeg_flush,
3585 .max_lowres = 3,3584 .max_lowres = 3,
3586 .pix_fmts = ff_h263_hwaccel_pixfmt_list_420,3585 .pix_fmts = ff_h263_hwaccel_pixfmt_list_420,
libavcodec/mpegvideo.c+21−12
@@ -932,17 +932,17 @@ av_cold int ff_mpv_common_init(MpegEncContext *s) · 931 unmodified lines
932 for (i = 0; i < MAX_PICTURE_COUNT; i++) {932 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
933 s->picture[i].f = av_frame_alloc();933 s->picture[i].f = av_frame_alloc();
934 if (!s->picture[i].f)934 if (!s->picture[i].f)
935 return AVERROR(ENOMEM);935 goto fail_nomem;
936 }936 }
937 937
938 if (!(s->next_picture.f = av_frame_alloc()) ||938 if (!(s->next_picture.f = av_frame_alloc()) ||
939 !(s->last_picture.f = av_frame_alloc()) ||939 !(s->last_picture.f = av_frame_alloc()) ||
940 !(s->current_picture.f = av_frame_alloc()) ||940 !(s->current_picture.f = av_frame_alloc()) ||
941 !(s->new_picture.f = av_frame_alloc()))941 !(s->new_picture.f = av_frame_alloc()))
942 return AVERROR(ENOMEM);942 goto fail_nomem;
943 943
944 if ((ret = init_context_frame(s)))944 if ((ret = init_context_frame(s)))
945 return AVERROR(ENOMEM);945 goto fail;
946 946
947 s->parse_context.state = -1;947 s->parse_context.state = -1;
948 948
@@ -956,10 +956,10 @@ av_cold int ff_mpv_common_init(MpegEncContext *s) · 7 unmodified lines
956 if (i) {956 if (i) {
957 s->thread_context[i] = av_memdup(s, sizeof(MpegEncContext));957 s->thread_context[i] = av_memdup(s, sizeof(MpegEncContext));
958 if (!s->thread_context[i])958 if (!s->thread_context[i])
959 return AVERROR(ENOMEM);959 goto fail_nomem;
960 }960 }
961 if ((ret = init_duplicate_context(s->thread_context[i])) < 0)961 if ((ret = init_duplicate_context(s->thread_context[i])) < 0)
962 return ret;962 goto fail;
963 s->thread_context[i]->start_mb_y =963 s->thread_context[i]->start_mb_y =
964 (s->mb_height * (i) + nb_slices / 2) / nb_slices;964 (s->mb_height * (i) + nb_slices / 2) / nb_slices;
965 s->thread_context[i]->end_mb_y =965 s->thread_context[i]->end_mb_y =
@@ -967,7 +967,7 @@ av_cold int ff_mpv_common_init(MpegEncContext *s) · 1 unmodified line
967 }967 }
968 } else {968 } else {
969 if ((ret = init_duplicate_context(s)) < 0)969 if ((ret = init_duplicate_context(s)) < 0)
970 return ret;970 goto fail;
971 s->start_mb_y = 0;971 s->start_mb_y = 0;
972 s->end_mb_y = s->mb_height;972 s->end_mb_y = s->mb_height;
973 }973 }
@@ -975,6 +975,11 @@ av_cold int ff_mpv_common_init(MpegEncContext *s) · 1 unmodified line
975// }975// }
976 976
977 return 0;977 return 0;
978 fail_nomem:
979 ret = AVERROR(ENOMEM);
980 fail:
981 ff_mpv_common_end(s);
982 return ret;
978}983}
979 984
980/**985/**
@@ -1067,17 +1072,17 @@ int ff_mpv_common_frame_size_change(MpegEncContext *s) · 86 unmodified lines
1067 1072
1068 if ((s->width || s->height) &&1073 if ((s->width || s->height) &&
1069 (err = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)1074 (err = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
1070 return err;1075 goto fail;
1071 1076
1072 /* set chroma shifts */1077 /* set chroma shifts */
1073 err = av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,1078 err = av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
1074 &s->chroma_x_shift,1079 &s->chroma_x_shift,
1075 &s->chroma_y_shift);1080 &s->chroma_y_shift);
1076 if (err < 0)1081 if (err < 0)
1077 return err;1082 goto fail;
1078 1083
1079 if ((err = init_context_frame(s)))1084 if ((err = init_context_frame(s)))
1080 return err;1085 goto fail;
1081 1086
1082 memset(s->thread_context, 0, sizeof(s->thread_context));1087 memset(s->thread_context, 0, sizeof(s->thread_context));
1083 s->thread_context[0] = s;1088 s->thread_context[0] = s;
@@ -1089,11 +1094,12 @@ int ff_mpv_common_frame_size_change(MpegEncContext *s) · 5 unmodified lines
1089 if (i) {1094 if (i) {
1090 s->thread_context[i] = av_memdup(s, sizeof(MpegEncContext));1095 s->thread_context[i] = av_memdup(s, sizeof(MpegEncContext));
1091 if (!s->thread_context[i]) {1096 if (!s->thread_context[i]) {
1092 return AVERROR(ENOMEM);1097 err = AVERROR(ENOMEM);
1098 goto fail;
1093 }1099 }
1094 }1100 }
1095 if ((err = init_duplicate_context(s->thread_context[i])) < 0)1101 if ((err = init_duplicate_context(s->thread_context[i])) < 0)
1096 return err;1102 goto fail;
1097 s->thread_context[i]->start_mb_y =1103 s->thread_context[i]->start_mb_y =
1098 (s->mb_height * (i) + nb_slices / 2) / nb_slices;1104 (s->mb_height * (i) + nb_slices / 2) / nb_slices;
1099 s->thread_context[i]->end_mb_y =1105 s->thread_context[i]->end_mb_y =
@@ -1102,7 +1108,7 @@ int ff_mpv_common_frame_size_change(MpegEncContext *s) · 2 unmodified lines
1102 } else {1108 } else {
1103 err = init_duplicate_context(s);1109 err = init_duplicate_context(s);
1104 if (err < 0)1110 if (err < 0)
1105 return err;1111 goto fail;
1106 s->start_mb_y = 0;1112 s->start_mb_y = 0;
1107 s->end_mb_y = s->mb_height;1113 s->end_mb_y = s->mb_height;
1108 }1114 }
@@ -1110,6 +1116,9 @@ int ff_mpv_common_frame_size_change(MpegEncContext *s) · 1 unmodified line
1110 }1116 }
1111 1117
1112 return 0;1118 return 0;
1119 fail:
1120 ff_mpv_common_end(s);
1121 return err;
1113}1122}
1114 1123
1115/* init common structure for both encoder and decoder */1124/* init common structure for both encoder and decoder */
libavcodec/msmpeg4dec.c+4−4
@@ -870,7 +870,7 @@ AVCodec ff_msmpeg4v1_decoder = { · 869 unmodified lines
870 .close = ff_h263_decode_end,870 .close = ff_h263_decode_end,
871 .decode = ff_h263_decode_frame,871 .decode = ff_h263_decode_frame,
872 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1,872 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1,
873 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM | FF_CODEC_CAP_INIT_CLEANUP,873 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
874 .max_lowres = 3,874 .max_lowres = 3,
875 .pix_fmts = (const enum AVPixelFormat[]) {875 .pix_fmts = (const enum AVPixelFormat[]) {
876 AV_PIX_FMT_YUV420P,876 AV_PIX_FMT_YUV420P,
@@ -888,7 +888,7 @@ AVCodec ff_msmpeg4v2_decoder = { · 11 unmodified lines
888 .close = ff_h263_decode_end,888 .close = ff_h263_decode_end,
889 .decode = ff_h263_decode_frame,889 .decode = ff_h263_decode_frame,
890 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1,890 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1,
891 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM | FF_CODEC_CAP_INIT_CLEANUP,891 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
892 .max_lowres = 3,892 .max_lowres = 3,
893 .pix_fmts = (const enum AVPixelFormat[]) {893 .pix_fmts = (const enum AVPixelFormat[]) {
894 AV_PIX_FMT_YUV420P,894 AV_PIX_FMT_YUV420P,
@@ -906,7 +906,7 @@ AVCodec ff_msmpeg4v3_decoder = { · 11 unmodified lines
906 .close = ff_h263_decode_end,906 .close = ff_h263_decode_end,
907 .decode = ff_h263_decode_frame,907 .decode = ff_h263_decode_frame,
908 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1,908 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1,
909 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM | FF_CODEC_CAP_INIT_CLEANUP,909 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
910 .max_lowres = 3,910 .max_lowres = 3,
911 .pix_fmts = (const enum AVPixelFormat[]) {911 .pix_fmts = (const enum AVPixelFormat[]) {
912 AV_PIX_FMT_YUV420P,912 AV_PIX_FMT_YUV420P,
@@ -924,7 +924,7 @@ AVCodec ff_wmv1_decoder = { · 11 unmodified lines
924 .close = ff_h263_decode_end,924 .close = ff_h263_decode_end,
925 .decode = ff_h263_decode_frame,925 .decode = ff_h263_decode_frame,
926 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1,926 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1,
927 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM | FF_CODEC_CAP_INIT_CLEANUP,927 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
928 .max_lowres = 3,928 .max_lowres = 3,
929 .pix_fmts = (const enum AVPixelFormat[]) {929 .pix_fmts = (const enum AVPixelFormat[]) {
930 AV_PIX_FMT_YUV420P,930 AV_PIX_FMT_YUV420P,
libavcodec/rv10.c+0−2
@@ -687,7 +687,6 @@ AVCodec ff_rv10_decoder = { · 686 unmodified lines
687 .close = rv10_decode_end,687 .close = rv10_decode_end,
688 .decode = rv10_decode_frame,688 .decode = rv10_decode_frame,
689 .capabilities = AV_CODEC_CAP_DR1,689 .capabilities = AV_CODEC_CAP_DR1,
690 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
691 .max_lowres = 3,690 .max_lowres = 3,
692 .pix_fmts = (const enum AVPixelFormat[]) {691 .pix_fmts = (const enum AVPixelFormat[]) {
693 AV_PIX_FMT_YUV420P,692 AV_PIX_FMT_YUV420P,
@@ -705,7 +704,6 @@ AVCodec ff_rv20_decoder = { · 11 unmodified lines
705 .close = rv10_decode_end,704 .close = rv10_decode_end,
706 .decode = rv10_decode_frame,705 .decode = rv10_decode_frame,
707 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,706 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
708 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
709 .flush = ff_mpeg_flush,707 .flush = ff_mpeg_flush,
710 .max_lowres = 3,708 .max_lowres = 3,
711 .pix_fmts = (const enum AVPixelFormat[]) {709 .pix_fmts = (const enum AVPixelFormat[]) {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t