I want to write a C++ application that opens a mp4 file and decode it as a yuv422 file. I wrote some code based on libavcodec tutorial but I couldn't find the place to set the bit depth and the format to YUV422.
here is the some of the code i wrote
void read_video_stream(AVFormatContext *pFormatContext, AVCodec *pCodec, AVCodecParameters *pCodecParameters,
int video_stream_index)
{
AVCodecContext *pCodecContext = avcodec_alloc_context3(pCodec);
std::unique_ptr<AVCodecContext, av_deleter> ctx_guard(pCodecContext);
if (!pCodecContext) {
return;
}
if (avcodec_parameters_to_context(pCodecContext, pCodecParameters) < 0) {
return;
}
// i tried setting it here
if (avcodec_open2(pCodecContext, pCodec, NULL) < 0) {
return;
}
while (true) {
std::unique_ptr<AVPacket, std::function<void(AVPacket*)>> packet{
new AVPacket,
[](AVPacket* p){ av_packet_unref(p); delete p; }};
av_init_packet(packet.get());
int response = av_read_frame(pFormatContext, packet.get());
if (AVERROR_EOF == response) {
std::cout << "EOF\n";
}
else if (response < 0) {
std::cout << "Error " << response;
return;
}
if (packet->stream_index != video_stream_index) {
continue;
}
response = avcodec_send_packet(pCodecContext, packet.get());
if (response < 0) {
std::cout << "Error while sending a packet to the decoder: " << response;
return;
}
while (response >= 0) {
std::shared_ptr<AVFrame> pFrame{ av_frame_alloc(), AVFrameDeleter};
response = avcodec_receive_frame(pCodecContext, pFrame.get());
if (response == AVERROR(EAGAIN)) {
continue;
}
if (response == AVERROR_EOF) {
std::cerr << "got to last frame\n";
return;
}
else if (response < 0) {
std::cerr << "Error while receiving a frame from the decoder: " << response;
return;
}
if (response >= 0) {
// copy line to cyclic buffer
cb.push_back(std::move(pFrame));
}
}
}
}
my end goal is to send the uncompressed data (need to be in pFrame->data[0-2]) to a device in the network. can you please help me with this issue thanks