Ffmepg multi-thread decoding process-2: avcodec_decode_video2

Source: Internet
Author: User

// Decoding function

Int attribute_align_arg avcodec_decode_video2 (avcodeccontext * avctx, avframe * picture,

Int * got_picture_ptr,
Const avpacket * avpkt)
{
Avcodecinternal * avci = avctx-> internal;
Int ret;
// Copy to ensure we do not change avpkt
Avpacket TMP = * avpkt;

If (avctx-> codec-> type! = Avmedia_type_video) {// exit if it is not of the video type
Av_log (avctx, av_log_error, "invalid media type for video \ n ");
Return averror (einval );
}

* Got_picture_ptr = 0 ;/
If (avctx-> coded_width | avctx-> coded_height) & av_image_check_size (avctx-> coded_width, avctx-> coded_height, 0, avctx ))
Return averror (einval );

Avcodec_get_frame_defaults (picture );

If (! Avctx-> refcounted_frames)
Av_frame_unref (& avci-> to_free );

// For frame parallel Decoding

// Set codec-> capabilities | = codec_cap_delay during codec initialization, view comments, and set avctx-> active_thread_type | = ff_thread_frame

If (avctx-> codec-> capabilities & codec_cap_delay) | avpkt-> size | (avctx-> active_thread_type & ff_thread_frame )){
Int did_split = av_packet_split_side_data (& TMP );
Apply_param_change (avctx, & TMP );

Avctx-> Pkt = & TMP;

// You need to configure -- enable-pthreads (my latest default config. H is # define have_threads 1) and set avctx-> active_thread_type | = ff_thread_frame

If (have_threads & avctx-> active_thread_type & ff_thread_frame)
Ret = ff_thread_decode_frame (avctx, picture, got_picture_ptr, & TMP); // enters frame-Level Parallel Decoding
Else {// enter Single-thread Decoding
Ret = avctx-> codec-> decode (avctx, picture, got_picture_ptr, & TMP );
Picture-> pkt_dts = avpkt-> DTS;

If (! Avctx-> has_ B _frames ){
Av_frame_set_pkt_pos (picture, avpkt-> POS );
}
// Fixme these shocould be under if (! Avctx-> has_ B _frames)
/* Get_buffer is supposed to set frame parameters */
If (! (Avctx-> codec-> capabilities & codec_cap_dr1 )){
If (! Picture-> sample_aspect_ratio.num) picture-> sample_aspect_ratio = avctx-> sample_aspect_ratio;
If (! Picture-> width) picture-> width = avctx-> width;
If (! Picture-> height) picture-> Height = avctx-> height;
If (picture-> Format = av_pix_fmt_none) picture-> Format = avctx-> pix_fmt;
}
}
Add_metadata_from_side_data (avctx, picture );

Emms_c (); // needed to avoid an emms_c () call before every return;

Avctx-> Pkt = NULL;
If (did_split ){
Ff_packet_free_side_data (& TMP );
If (ret = TMP. Size)
Ret = avpkt-> size;
}

If (Ret <0 & picture-> data [0])
Av_frame_unref (picture );

If (* got_picture_ptr ){
If (! Avctx-> refcounted_frames ){
Avci-> to_free = * picture;
Avci-> to_free.extended_data = avci-> to_free.data;
}

Avctx-> frame_number ++;
Av_frame_set_best_effort_timestamp (picture,
Guess_correct_pts (avctx,
Picture-> pkt_pts,
Picture-> pkt_dts ));
}
} Else
Ret = 0;

/* Includecoders assign whole avframes, thus overwriting extended_data;

* Make sure it's set correctly */

// Many decoders allocate the entire avframes to overwrite extended_data

Picture-> extended_data = picture-> data;

Return ret;
}

 

Note:

/**
* Encoder or decoder requires flushing with null input at the end in order
* Give the complete and correct output.
*
* Note: If this flag is not set, the codec is guaranteed to never be fed
* With NULL data. The user can still send NULL data to the public encode
* Or decode function, but libavcodec will not pass it along to the Codec
* Unless this flag is set.
*
* Decoders:
* The decoder has a non-zero delay and needs to be fed with avpkt-> DATA = NULL,
* Avpkt-> size = 0 at the end to get the delayed data until the decoder no longer
* Returns frames.
*
* Encoders:
* The encoder needs to be fed with NULL data at the end of encoding until
* Encoder no longer returns data.
*
* Note: For encoders implementing the avcodec. encode2 () function, setting this
* Flag also means that the encoder must set the PTS and duration
* Each output packet. If this flag is not set, the PTS and duration will
* Be determined by libavcodec from the input frame.
*/

# Define codec_cap_delay 0x0020

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.