| @@ -179,6 +179,116 @@ static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame) · 178 unmodified lines |
| 179 | return 0; | 179 | return 0; |
| 180 | } | 180 | } |
| 181 | | 181 | |
| | 182 | static int bsfs_init(AVCodecContext *avctx) |
| | 183 | { |
| | 184 | AVCodecInternal *avci = avctx->internal; |
| | 185 | DecodeFilterContext *s = &avci->filter; |
| | 186 | const char *bsfs_str; |
| | 187 | int ret; |
| | 188 | |
| | 189 | if (s->nb_bsfs) |
| | 190 | return 0; |
| | 191 | |
| | 192 | bsfs_str = avctx->codec->bsfs ? avctx->codec->bsfs : "null"; |
| | 193 | while (bsfs_str && *bsfs_str) { |
| | 194 | AVBSFContext **tmp; |
| | 195 | const AVBitStreamFilter *filter; |
| | 196 | char *bsf; |
| | 197 | |
| | 198 | bsf = av_get_token(&bsfs_str, ","); |
| | 199 | if (!bsf) { |
| | 200 | ret = AVERROR(ENOMEM); |
| | 201 | goto fail; |
| | 202 | } |
| | 203 | |
| | 204 | filter = av_bsf_get_by_name(bsf); |
| | 205 | if (!filter) { |
| | 206 | av_log(avctx, AV_LOG_ERROR, "A non-existing bitstream filter %s " |
| | 207 | "requested by a decoder. This is a bug, please report it.\n", |
| | 208 | bsf); |
| | 209 | ret = AVERROR_BUG; |
| | 210 | av_freep(&bsf); |
| | 211 | goto fail; |
| | 212 | } |
| | 213 | av_freep(&bsf); |
| | 214 | |
| | 215 | tmp = av_realloc_array(s->bsfs, s->nb_bsfs + 1, sizeof(*s->bsfs)); |
| | 216 | if (!tmp) { |
| | 217 | ret = AVERROR(ENOMEM); |
| | 218 | goto fail; |
| | 219 | } |
| | 220 | s->bsfs = tmp; |
| | 221 | s->nb_bsfs++; |
| | 222 | |
| | 223 | ret = av_bsf_alloc(filter, &s->bsfs[s->nb_bsfs - 1]); |
| | 224 | if (ret < 0) |
| | 225 | goto fail; |
| | 226 | |
| | 227 | if (s->nb_bsfs == 1) { |
| | 228 | /* We do not currently have an API for passing the input timebase into decoders, |
| | 229 | * but no filters used here should actually need it. |
| | 230 | * So we make up some plausible-looking number (the MPEG 90kHz timebase) */ |
| | 231 | s->bsfs[s->nb_bsfs - 1]->time_base_in = (AVRational){ 1, 90000 }; |
| | 232 | ret = avcodec_parameters_from_context(s->bsfs[s->nb_bsfs - 1]->par_in, |
| | 233 | avctx); |
| | 234 | } else { |
| | 235 | s->bsfs[s->nb_bsfs - 1]->time_base_in = s->bsfs[s->nb_bsfs - 2]->time_base_out; |
| | 236 | ret = avcodec_parameters_copy(s->bsfs[s->nb_bsfs - 1]->par_in, |
| | 237 | s->bsfs[s->nb_bsfs - 2]->par_out); |
| | 238 | } |
| | 239 | if (ret < 0) |
| | 240 | goto fail; |
| | 241 | |
| | 242 | ret = av_bsf_init(s->bsfs[s->nb_bsfs - 1]); |
| | 243 | if (ret < 0) |
| | 244 | goto fail; |
| | 245 | } |
| | 246 | |
| | 247 | return 0; |
| | 248 | fail: |
| | 249 | ff_decode_bsfs_uninit(avctx); |
| | 250 | return ret; |
| | 251 | } |
| | 252 | |
| | 253 | /* try to get one output packet from the filter chain */ |
| | 254 | static int bsfs_poll(AVCodecContext *avctx, AVPacket *pkt) |
| | 255 | { |
| | 256 | DecodeFilterContext *s = &avctx->internal->filter; |
| | 257 | int idx, ret; |
| | 258 | |
| | 259 | /* start with the last filter in the chain */ |
| | 260 | idx = s->nb_bsfs - 1; |
| | 261 | while (idx >= 0) { |
| | 262 | /* request a packet from the currently selected filter */ |
| | 263 | ret = av_bsf_receive_packet(s->bsfs[idx], pkt); |
| | 264 | if (ret == AVERROR(EAGAIN)) { |
| | 265 | /* no packets available, try the next filter up the chain */ |
| | 266 | ret = 0; |
| | 267 | idx--; |
| | 268 | continue; |
| | 269 | } else if (ret < 0 && ret != AVERROR_EOF) { |
| | 270 | return ret; |
| | 271 | } |
| | 272 | |
| | 273 | /* got a packet or EOF -- pass it to the caller or to the next filter |
| | 274 | * down the chain */ |
| | 275 | if (idx == s->nb_bsfs - 1) { |
| | 276 | return ret; |
| | 277 | } else { |
| | 278 | idx++; |
| | 279 | ret = av_bsf_send_packet(s->bsfs[idx], ret < 0 ? NULL : pkt); |
| | 280 | if (ret < 0) { |
| | 281 | av_log(avctx, AV_LOG_ERROR, |
| | 282 | "Error pre-processing a packet before decoding\n"); |
| | 283 | av_packet_unref(pkt); |
| | 284 | return ret; |
| | 285 | } |
| | 286 | } |
| | 287 | } |
| | 288 | |
| | 289 | return AVERROR(EAGAIN); |
| | 290 | } |
| | 291 | |
| 182 | int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt) | 292 | int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt) |
| 183 | { | 293 | { |
| 184 | AVCodecInternal *avci = avctx->internal; | 294 | AVCodecInternal *avci = avctx->internal; |
| @@ -187,10 +297,11 @@ int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt) · 2 unmodified lines |
| 187 | if (avci->draining) | 297 | if (avci->draining) |
| 188 | return AVERROR_EOF; | 298 | return AVERROR_EOF; |
| 189 | | 299 | |
| 190 | if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data_elems) | 300 | ret = bsfs_poll(avctx, pkt); |
| 191 | return AVERROR(EAGAIN); | 301 | if (ret == AVERROR_EOF) |
| 192 | | 302 | avci->draining = 1; |
| 193 | av_packet_move_ref(pkt, avci->buffer_pkt); | 303 | if (ret < 0) |
| | 304 | return ret; |
| 194 | | 305 | |
| 195 | ret = extract_packet_props(avctx->internal, pkt); | 306 | ret = extract_packet_props(avctx->internal, pkt); |
| 196 | if (ret < 0) | 307 | if (ret < 0) |
| @@ -525,20 +636,26 @@ int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacke · 328 unmodified lines |
| 525 | if (avctx->internal->draining) | 636 | if (avctx->internal->draining) |
| 526 | return AVERROR_EOF; | 637 | return AVERROR_EOF; |
| 527 | | 638 | |
| 528 | if (avci->buffer_pkt->data || avci->buffer_pkt->side_data_elems) | | |
| 529 | return AVERROR(EAGAIN); | | |
| 530 | | | |
| 531 | if (avpkt && !avpkt->size && avpkt->data) | 639 | if (avpkt && !avpkt->size && avpkt->data) |
| 532 | return AVERROR(EINVAL); | 640 | return AVERROR(EINVAL); |
| 533 | | 641 | |
| 534 | if (!avpkt || !avpkt->size) { | 642 | ret = bsfs_init(avctx); |
| 535 | avctx->internal->draining = 1; | 643 | if (ret < 0) |
| 536 | } else { | 644 | return ret; |
| | 645 | |
| | 646 | av_packet_unref(avci->buffer_pkt); |
| | 647 | if (avpkt && (avpkt->data || avpkt->side_data_elems)) { |
| 537 | ret = av_packet_ref(avci->buffer_pkt, avpkt); | 648 | ret = av_packet_ref(avci->buffer_pkt, avpkt); |
| 538 | if (ret < 0) | 649 | if (ret < 0) |
| 539 | return ret; | 650 | return ret; |
| 540 | } | 651 | } |
| 541 | | 652 | |
| | 653 | ret = av_bsf_send_packet(avci->filter.bsfs[0], avci->buffer_pkt); |
| | 654 | if (ret < 0) { |
| | 655 | av_packet_unref(avci->buffer_pkt); |
| | 656 | return ret; |
| | 657 | } |
| | 658 | |
| 542 | if (!avci->buffer_frame->buf[0]) { | 659 | if (!avci->buffer_frame->buf[0]) { |
| 543 | ret = decode_receive_frame_internal(avctx, avci->buffer_frame); | 660 | ret = decode_receive_frame_internal(avctx, avci->buffer_frame); |
| 544 | if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) | 661 | if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) |
| @@ -558,6 +675,10 @@ int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *fr · 13 unmodified lines |
| 558 | if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec)) | 675 | if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec)) |
| 559 | return AVERROR(EINVAL); | 676 | return AVERROR(EINVAL); |
| 560 | | 677 | |
| | 678 | ret = bsfs_init(avctx); |
| | 679 | if (ret < 0) |
| | 680 | return ret; |
| | 681 | |
| 561 | if (avci->buffer_frame->buf[0]) { | 682 | if (avci->buffer_frame->buf[0]) { |
| 562 | av_frame_move_ref(frame, avci->buffer_frame); | 683 | av_frame_move_ref(frame, avci->buffer_frame); |
| 563 | } else { | 684 | } else { |
| @@ -630,13 +751,18 @@ static int compat_decode(AVCodecContext *avctx, AVFrame *frame, · 66 unmodified lines |
| 630 | } | 751 | } |
| 631 | } | 752 | } |
| 632 | | 753 | |
| 633 | if (avci->draining || avci->compat_decode_consumed < pkt->size) | 754 | if (avci->draining || (!avctx->codec->bsfs && avci->compat_decode_consumed < pkt->size)) |
| 634 | break; | 755 | break; |
| 635 | } | 756 | } |
| 636 | | 757 | |
| 637 | finish: | 758 | finish: |
| 638 | if (ret == 0) | 759 | if (ret == 0) { |
| 639 | ret = FFMIN(avci->compat_decode_consumed, pkt->size); | 760 | /* if there are any bsfs then assume full packet is always consumed */ |
| | 761 | if (avctx->codec->bsfs) |
| | 762 | ret = pkt->size; |
| | 763 | else |
| | 764 | ret = FFMIN(avci->compat_decode_consumed, pkt->size); |
| | 765 | } |
| 640 | avci->compat_decode_consumed = 0; | 766 | avci->compat_decode_consumed = 0; |
| 641 | avci->compat_decode_partial_size = (ret >= 0) ? pkt->size - ret : 0; | 767 | avci->compat_decode_partial_size = (ret >= 0) ? pkt->size - ret : 0; |
| 642 | | 768 | |
| @@ -1549,6 +1675,19 @@ void avcodec_flush_buffers(AVCodecContext *avctx) · 906 unmodified lines |
| 1549 | avctx->pts_correction_last_pts = | 1675 | avctx->pts_correction_last_pts = |
| 1550 | avctx->pts_correction_last_dts = INT64_MIN; | 1676 | avctx->pts_correction_last_dts = INT64_MIN; |
| 1551 | | 1677 | |
| | 1678 | ff_decode_bsfs_uninit(avctx); |
| | 1679 | |
| 1552 | if (!avctx->refcounted_frames) | 1680 | if (!avctx->refcounted_frames) |
| 1553 | av_frame_unref(avctx->internal->to_free); | 1681 | av_frame_unref(avctx->internal->to_free); |
| 1554 | } | 1682 | } |
| | 1683 | |
| | 1684 | void ff_decode_bsfs_uninit(AVCodecContext *avctx) |
| | 1685 | { |
| | 1686 | DecodeFilterContext *s = &avctx->internal->filter; |
| | 1687 | int i; |
| | 1688 | |
| | 1689 | for (i = 0; i < s->nb_bsfs; i++) |
| | 1690 | av_bsf_free(&s->bsfs[i]); |
| | 1691 | av_freep(&s->bsfs); |
| | 1692 | s->nb_bsfs = 0; |
| | 1693 | } |