Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static int decode_packet(int *got_frame, int cached)
- {
- int ret = 0;
- int decoded = pkt.size;
- *got_frame = 0;
- if (pkt.stream_index == video_stream_idx) {
- /* decode video frame */
- ret = avcodec_send_packet(video_dec_ctx, &pkt);
- if (ret < 0) {
- fprintf(stderr, "Error submitting the packet to the decoder\n");
- exit(1);
- }
- /* read all the output frames (in general there may be any number of them */
- while (ret >= 0) {
- ret = avcodec_receive_frame(video_dec_ctx, frame);
- if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
- return decoded;
- else if (ret < 0) {
- fprintf(stderr, "Error during decoding\n");
- exit(1);
- }
- if (frame->width != width || frame->height != height ||
- frame->format != pix_fmt) {
- /* To handle this change, one could call av_image_alloc again and
- * decode the following frames into another rawvideo file. */
- fprintf(stderr, "Error: Width, height and pixel format have to be "
- "constant in a rawvideo file, but the width, height or "
- "pixel format of the input video changed:\n"
- "old: width = %d, height = %d, format = %s\n"
- "new: width = %d, height = %d, format = %s\n",
- width, height, av_get_pix_fmt_name(pix_fmt),
- frame->width, frame->height,
- av_get_pix_fmt_name((AVPixelFormat)frame->format));
- return -1;
- }
- printf("video_frame%s n:%d coded_n:%d\n",
- cached ? "(cached)" : "",
- video_frame_count++, frame->coded_picture_number);
- /* copy decoded frame to destination buffer:
- * this is required since rawvideo expects non aligned data */
- av_image_copy(video_dst_data, video_dst_linesize,
- (const uint8_t **)(frame->data), frame->linesize,
- pix_fmt, width, height);
- /* write to rawvideo file */
- //fwrite(video_dst_data[0], 1, video_dst_bufsize, video_dst_file);
- }
- }
- else if (pkt.stream_index == audio_stream_idx) {
- /* decode audio frame */
- ret = avcodec_send_packet(audio_dec_ctx, &pkt);
- if (ret < 0) {
- fprintf(stderr, "Error submitting the packet to the decoder\n");
- exit(1);
- }
- /* read all the output frames (in general there may be any number of them */
- while (ret >= 0) {
- ret = avcodec_receive_frame(audio_dec_ctx, frame);
- if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
- return decoded;
- else if (ret < 0) {
- fprintf(stderr, "Error during decoding\n");
- exit(1);
- }
- size_t unpadded_linesize = frame->nb_samples * av_get_bytes_per_sample((AVSampleFormat)frame->format);
- /*printf("audio_frame%s n:%d nb_samples:%d pts:%s\n",
- cached ? "(cached)" : "",
- audio_frame_count++, frame->nb_samples,
- av_ts2timestr(frame->pts, &audio_dec_ctx->time_base));*/
- /* Write the raw audio data samples of the first plane. This works
- * fine for packed formats (e.g. AV_SAMPLE_FMT_S16). However,
- * most audio decoders output planar audio, which uses a separate
- * plane of audio samples for each channel (e.g. AV_SAMPLE_FMT_S16P).
- * In other words, this code will write only the first audio channel
- * in these cases.
- * You should use libswresample or libavfilter to convert the frame
- * to packed data. */
- fwrite(frame->extended_data[0], 1, unpadded_linesize, audio_dst_file);
- }
- }
- /* If we use frame reference counting, we own the data and need
- * to de-reference it when we don't use it anymore */
- if (*got_frame && refcount)
- av_frame_unref(frame);
- return decoded;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement