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
| #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libavutil/opt.h>
static int ptsIndex = 0;
void encode(AVCodecContext *pContext, AVFrame *pFrame, AVPacket *packet, FILE *pOutFile) { int ret = 0; if (pFrame) pFrame->pts = ptsIndex++;
ret = avcodec_send_frame(pContext, pFrame); if (ret < 0) { fprintf(stderr, "Error sending the frame to the encoder\n"); return; }
while (ret >= 0) {return ret = avcodec_receive_packet(pContext, packet); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) return; else if (ret < 0) { fprintf(stderr, "Error during encoding\n"); break; }
packet->stream_index = 0;
fwrite(packet->data, 1, packet->size, pOutFile);
av_packet_unref(packet); } }
int main(int argc, char *argv[]) { const char *filename, *outfilename;
const AVCodec *pCodec = NULL; AVCodecContext *pContext = NULL;
FILE *pInputFile; FILE *pOutFile;
AVFrame *pFrame = NULL; AVPacket *pPacket = NULL;
int ret = 0;
filename = "output.yuv"; outfilename = "enc.h264"; do { pCodec = avcodec_find_encoder(AV_CODEC_ID_H264); if (!pCodec) { fprintf(stderr, "Codec not found\n"); break; }
pContext = avcodec_alloc_context3(pCodec); if (!pContext) { fprintf(stderr, "Could not allocate video codec context\n"); break; }
pContext->bit_rate = 400000; pContext->width = 352; pContext->height = 288;
pContext->framerate = (AVRational){30, 1}; pContext->time_base = (AVRational){1, 30};
pContext->gop_size = 12; pContext->max_b_frames = 0; pContext->pix_fmt = AV_PIX_FMT_YUV420P;
if (avcodec_open2(pContext, pCodec, NULL) < 0) { fprintf(stderr, "Could not open codec: %s\n", av_err2str(ret)); break; }
pInputFile = fopen(filename, "rb"); if (!pInputFile) { fprintf(stderr, "Could not open %s\n", filename); break; }
pOutFile = fopen(outfilename, "wb"); if (!pOutFile) { fprintf(stderr, "Could not open %s\n", outfilename); break; }
pPacket = av_packet_alloc(); if (!pPacket) { break; }
pFrame = av_frame_alloc(); if (!pFrame) { fprintf(stderr, "Could not allocate video frame\n"); break; }
pFrame->format = pContext->pix_fmt; pFrame->height = pContext->height; pFrame->width = pContext->width;
ret = av_frame_get_buffer(pFrame, 1); if (ret != 0) { printf("frame get buffer fail\n"); break; }
while (!feof(pInputFile)) { fread(pFrame->data[0], 1, pFrame->width * pFrame->height, pInputFile); fread(pFrame->data[1], 1, pFrame->width * pFrame->height / 4, pInputFile); fread(pFrame->data[2], 1, pFrame->width * pFrame->height / 4, pInputFile);
encode(pContext, pFrame, pPacket, pOutFile); } } while (0);
encode(pContext, NULL, pPacket, pOutFile);
fclose(pInputFile); fclose(pOutFile); avcodec_free_context(&pContext); av_frame_free(&pFrame); av_packet_free(&pPacket);
return 0; }
|