| 140 | } |
| 141 | |
| 142 | static int dec_enc(AVPacket *pkt, const AVCodec *enc_codec) |
| 143 | { |
| 144 | AVFrame *frame; |
| 145 | int ret = 0; |
| 146 | |
| 147 | ret = avcodec_send_packet(decoder_ctx, pkt); |
| 148 | if (ret < 0) { |
| 149 | fprintf(stderr, "Error during decoding. Error code: %s\n", av_err2str(ret)); |
| 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | while (ret >= 0) { |
| 154 | if (!(frame = av_frame_alloc())) |
| 155 | return AVERROR(ENOMEM); |
| 156 | |
| 157 | ret = avcodec_receive_frame(decoder_ctx, frame); |
| 158 | if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { |
| 159 | av_frame_free(&frame); |
| 160 | return 0; |
| 161 | } else if (ret < 0) { |
| 162 | fprintf(stderr, "Error while decoding. Error code: %s\n", av_err2str(ret)); |
| 163 | goto fail; |
| 164 | } |
| 165 | |
| 166 | if (!initialized) { |
| 167 | /* we need to ref hw_frames_ctx of decoder to initialize encoder's codec. |
| 168 | Only after we get a decoded frame, can we obtain its hw_frames_ctx */ |
| 169 | encoder_ctx->hw_frames_ctx = av_buffer_ref(decoder_ctx->hw_frames_ctx); |
| 170 | if (!encoder_ctx->hw_frames_ctx) { |
| 171 | ret = AVERROR(ENOMEM); |
| 172 | goto fail; |
| 173 | } |
| 174 | /* set AVCodecContext Parameters for encoder, here we keep them stay |
| 175 | * the same as decoder. |
| 176 | * xxx: now the sample can't handle resolution change case. |
| 177 | */ |
| 178 | encoder_ctx->time_base = av_inv_q(decoder_ctx->framerate); |
| 179 | encoder_ctx->pix_fmt = AV_PIX_FMT_VAAPI; |
| 180 | encoder_ctx->width = decoder_ctx->width; |
| 181 | encoder_ctx->height = decoder_ctx->height; |
| 182 | |
| 183 | if ((ret = avcodec_open2(encoder_ctx, enc_codec, NULL)) < 0) { |
| 184 | fprintf(stderr, "Failed to open encode codec. Error code: %s\n", |
| 185 | av_err2str(ret)); |
| 186 | goto fail; |
| 187 | } |
| 188 | |
| 189 | if (!(ost = avformat_new_stream(ofmt_ctx, enc_codec))) { |
| 190 | fprintf(stderr, "Failed to allocate stream for output format.\n"); |
| 191 | ret = AVERROR(ENOMEM); |
| 192 | goto fail; |
| 193 | } |
| 194 | |
| 195 | ost->time_base = encoder_ctx->time_base; |
| 196 | ret = avcodec_parameters_from_context(ost->codecpar, encoder_ctx); |
| 197 | if (ret < 0) { |
| 198 | fprintf(stderr, "Failed to copy the stream parameters. " |
| 199 | "Error code: %s\n", av_err2str(ret)); |
no test coverage detected