Apollo 11.0
自动驾驶开放平台
h265_decoder.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2019 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
17#include <algorithm>
18
19#include "cyber/common/log.h"
21
22namespace apollo {
23namespace drivers {
24namespace video {
25
26// Using to-be-deprecated avcodec_decode_video2 for now until its replacement
27// gets stable
28#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
29
31 avcodec_register_all();
32 AVCodec* codec_h265 = avcodec_find_decoder(AV_CODEC_ID_H265);
33 if (codec_h265 == nullptr) {
34 AERROR << "error: codec not found";
35 return false;
36 }
37 codec_ctx_h265_ = avcodec_alloc_context3(codec_h265);
38 if (codec_ctx_h265_ == nullptr) {
39 AERROR << "error: codec context alloc fail";
40 return false;
41 }
42 if (avcodec_open2(codec_ctx_h265_, codec_h265, nullptr) < 0) {
43 AERROR << "error: could not open codec";
44 return false;
45 }
46 yuv_frame_ = av_frame_alloc();
47 if (yuv_frame_ == nullptr) {
48 AERROR << "error: could not alloc yuv frame";
49 return false;
50 }
51 AVCodec* codec_jpeg = avcodec_find_encoder(AV_CODEC_ID_MJPEG);
52 if (codec_jpeg == nullptr) {
53 AERROR << "error: jpeg Codec not found";
54 return false;
55 }
56 codec_ctx_jpeg_ = avcodec_alloc_context3(codec_jpeg);
57 if (codec_ctx_jpeg_ == nullptr) {
58 AERROR << "error: jpeg ctx allco fail";
59 return false;
60 }
61 // Put sample parameters and open it
62 codec_ctx_jpeg_->bit_rate = 400000;
63 codec_ctx_jpeg_->codec_type = AVMEDIA_TYPE_VIDEO;
64 codec_ctx_jpeg_->codec_id = AV_CODEC_ID_MJPEG;
65 codec_ctx_jpeg_->width = 1920;
66 codec_ctx_jpeg_->height = 1080;
67 codec_ctx_jpeg_->time_base = (AVRational){1, 15};
68 codec_ctx_jpeg_->pix_fmt = AV_PIX_FMT_YUVJ422P;
69 if (avcodec_open2(codec_ctx_jpeg_, codec_jpeg, nullptr) < 0) {
70 AERROR << "error: could not open jpeg context";
71 return false;
72 }
73 return true;
74}
75
76void H265Decoder::Release() {
77 if (codec_ctx_h265_ != nullptr) {
78 avcodec_free_context(&codec_ctx_h265_);
79 codec_ctx_h265_ = nullptr;
80 }
81 if (yuv_frame_ != nullptr) {
82 av_frame_free(&yuv_frame_);
83 yuv_frame_ = nullptr;
84 }
85 if (codec_ctx_jpeg_ != nullptr) {
86 avcodec_free_context(&codec_ctx_jpeg_);
87 codec_ctx_jpeg_ = nullptr;
88 }
89}
90
92 const uint8_t* indata, const int32_t insize,
93 std::vector<uint8_t>* outdata) const {
94 AVPacket apt;
95 outdata->clear();
96 av_init_packet(&apt);
97 int got_picture = 0;
98 apt.data = const_cast<uint8_t*>(indata);
99 apt.size = insize;
100 if (apt.size == 0) {
101 apt.data = nullptr;
102 }
103 int ret =
104 avcodec_decode_video2(codec_ctx_h265_, yuv_frame_, &got_picture, &apt);
105 if (ret < 0) {
106 AERROR << "error: decode failed: input_framesize = " << apt.size
107 << ". error code = " << ret;
109 }
110 if (!got_picture) {
111 // Not an error, but just did not read pictures out
112 AWARN << "warn: failed to get yuv picture";
114 }
115 av_packet_unref(&apt);
116 got_picture = 0;
117 ret = avcodec_encode_video2(codec_ctx_jpeg_, &apt, yuv_frame_, &got_picture);
118 if (ret < 0) {
119 AERROR << "error: jpeg encode failed, error code = " << ret;
121 }
122 if (!got_picture) {
123 AERROR << "error: failed to get jpeg picture from yuyv";
125 }
126 outdata->resize(apt.size);
127 std::copy(apt.data, apt.data + apt.size, outdata->begin());
129}
130
131} // namespace video
132} // namespace drivers
133} // namespace apollo
DecodingResult Process(const uint8_t *indata, const int32_t insize, std::vector< uint8_t > *outdata) const
#define AERROR
Definition log.h:44
#define AWARN
Definition log.h:43
class register implement
Definition arena_queue.h:37
#define AV_CODEC_ID_MJPEG
Definition usb_cam.h:60