1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
| #include "SDCEncoder.h" #ifdef __cplusplus extern "C" { #endif #include "libavformat/avformat.h" #include "libavformat/avio.h" #include "libavcodec/avcodec.h" #include <libavutil/imgutils.h> #ifdef __cplusplus }; #endif
static int is_released = 1;
static int framecnt;
static int encoder_h264_frame_width;
static int encoder_h264_frame_height;
static int y_size;
static unsigned char *picture_buf;
static int picture_size;
static AVFormatContext *pFormatCtx;
static AVOutputFormat *fmt;
static AVStream *video_st;
static AVCodecContext *pCodecCtx;
static struct AVCodec *pCodec;
static AVFrame *pFrame;
static AVPacket pkt;
int sd_init_encoder(int width, int height, int bitrate, const char * out_file) { is_released = 0; framecnt = 0; encoder_h264_frame_width = width; encoder_h264_frame_height = height; av_register_all(); if (avformat_alloc_output_context2(&pFormatCtx, NULL, NULL, out_file) < 0) { printf("Failed to alloc AVFormatContext! \n"); return -1; } fmt = pFormatCtx->oformat; if (avio_open( &pFormatCtx->pb, out_file, AVIO_FLAG_READ_WRITE) < 0){ printf("Failed to open output file! \n"); return -1; } video_st = avformat_new_stream(pFormatCtx, NULL); video_st->time_base.num = 1; video_st->time_base.den = 30; if (video_st==NULL){ return -1; } pCodecCtx = avcodec_alloc_context3(pCodec);
pCodecCtx->codec_id = fmt->video_codec; pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO; pCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P; pCodecCtx->width = encoder_h264_frame_width; pCodecCtx->height = encoder_h264_frame_height; pCodecCtx->time_base.num = 1; pCodecCtx->time_base.den = 30; pCodecCtx->bit_rate = bitrate; pCodecCtx->qmin = 10; pCodecCtx->qmax = 51; pCodecCtx->gop_size = 250; pCodecCtx->max_b_frames = 0; AVDictionary *param = NULL; if(pCodecCtx->codec_id == AV_CODEC_ID_H264) { av_dict_set(¶m, "preset", "slow", 0); av_dict_set(¶m, "tune", "zerolatency", 0); } pCodec = avcodec_find_encoder(pCodecCtx->codec_id); if (!pCodec) { printf("Can not find encoder!"); } if (avcodec_open2(pCodecCtx, pCodec, ¶m) < 0) { printf("Failed to open encoder!"); } avcodec_parameters_from_context(video_st->codecpar, pCodecCtx); av_dump_format(pFormatCtx, 0, out_file, 1); pFrame = av_frame_alloc(); pFrame->width = pCodecCtx->width; pFrame->height = pCodecCtx->height; pFrame->format = AV_PIX_FMT_YUV420P; pFrame->color_range = AVCOL_RANGE_MPEG; av_image_fill_arrays(pFrame->data, pFrame->linesize, picture_buf, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, 1); if (AVSTREAM_INIT_IN_WRITE_HEADER != avformat_write_header(pFormatCtx, NULL) ) { printf("avformat_write_header fail \n"); return -1; } av_new_packet(&pkt, picture_size); y_size = pCodecCtx->width * pCodecCtx->height; return 0; }
void sd_free_resource(void) { if (1 == is_released) { return; } av_write_trailer(pFormatCtx); if (pCodecCtx){ avcodec_close(pCodecCtx); avcodec_free_context(&pCodecCtx); pCodecCtx = NULL; } if (pFrame) { av_free(pFrame); pFrame = NULL; } avio_close(pFormatCtx->pb); avformat_free_context(pFormatCtx); framecnt = 0; is_released = 1; }
int sd_is_encoder_valid(void) { if (pCodecCtx) { return 1; } return 0; }
void sd_encode(unsigned char *data) { pFrame->data[0] = data; pFrame->data[1] = pFrame->data[0] + y_size; pFrame->data[2] = pFrame->data[1] + y_size / 4; pFrame->pts = framecnt; int ret = avcodec_send_frame(pCodecCtx, pFrame); while (ret == 0) { ret = avcodec_receive_packet(pCodecCtx, &pkt); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) return; else if (ret < 0) { printf("Error during encoding\n"); return; } printf("Succeed to encode frame: %5d\tsize:%5db\n", framecnt, pkt.size); framecnt++; pkt.stream_index = video_st->index; ret = av_write_frame(pFormatCtx, &pkt); av_packet_unref(&pkt); } }
int sd_flush_encode() { if (1 == is_released) { return -1; } int ret = -1; AVPacket enc_pkt; ret = avcodec_send_frame(pCodecCtx, NULL); if (ret != 0) { return -1; } int got_pic = 0; while (got_pic == 0) { enc_pkt.data = NULL; enc_pkt.size = 0; av_init_packet(&enc_pkt); got_pic = avcodec_receive_packet(pCodecCtx, &enc_pkt); if (got_pic == AVERROR(EAGAIN) || got_pic == AVERROR_EOF) return -1; else if (got_pic < 0) { printf("Error during encoding\n"); return -1; } printf("Succeed to flush frame: %5d\tsize:%5d \n", framecnt, enc_pkt.size); framecnt++; enc_pkt.stream_index = video_st->index; ret = av_write_frame(pFormatCtx, &enc_pkt); av_packet_unref(&enc_pkt); if (ret < 0) { break; } } return ret; }
|