1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| static int decode_write(AVCodecContext *avctx, AVPacket *packet) { ret = avcodec_send_packet(avctx, packet); if (ret < 0) { return ret; } while (1) { if (!(frame = av_frame_alloc()) || !(sw_frame = av_frame_alloc())) { ret = AVERROR(ENOMEM); goto fail; } ret = avcodec_receive_frame(avctx, frame); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { av_frame_free(&frame); av_frame_free(&sw_frame); return 0; } else if (ret < 0) { goto fail; } if (frame->format == hw_pix_fmt) { if ((ret = av_hwframe_transfer_data(sw_frame, frame, 0)) < 0) { goto fail; } tmp_frame = sw_frame; } else tmp_frame = frame; size = av_image_get_buffer_size(tmp_frame->format, tmp_frame->width, tmp_frame->height, 1); buffer = av_malloc(size); if (!buffer) { ret = AVERROR(ENOMEM); goto fail; } ret = av_image_copy_to_buffer(buffer, size, (const uint8_t * const *)tmp_frame->data, (const int *)tmp_frame->linesize, tmp_frame->format, tmp_frame->width, tmp_frame->height, 1); if (ret < 0) { goto fail; } if ((ret = fwrite(buffer, 1, size, output_file)) < 0) { goto fail; } fail: av_frame_free(&frame); av_frame_free(&sw_frame); av_freep(&buffer); if (ret < 0) return ret; } }
|