Mercurial > libavcodec.hg
annotate libschroedingerenc.c @ 11683:83d14d771e80 libavcodec
schroenc: Set open-gop
| author | conrad |
|---|---|
| date | Wed, 05 May 2010 21:02:49 +0000 |
| parents | 5173cc26d42f |
| children | b4aff04c10cf |
| rev | line source |
|---|---|
| 6738 | 1 /* |
| 2 * Dirac encoder support via Schroedinger libraries | |
| 3 * Copyright (c) 2008 BBC, Anuradha Suraparaju <asuraparaju at gmail dot com > | |
| 4 * | |
| 5 * This file is part of FFmpeg. | |
| 6 * | |
| 7 * FFmpeg is free software; you can redistribute it and/or | |
| 8 * modify it under the terms of the GNU Lesser General Public | |
| 9 * License as published by the Free Software Foundation; either | |
| 10 * version 2.1 of the License, or (at your option) any later version. | |
| 11 * | |
| 12 * FFmpeg is distributed in the hope that it will be useful, | |
| 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 15 * Lesser General Public License for more details. | |
| 16 * | |
| 17 * You should have received a copy of the GNU Lesser General Public | |
| 18 * License along with FFmpeg; if not, write to the Free Software | |
| 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
| 20 */ | |
| 21 | |
| 22 /** | |
|
11644
7dd2a45249a9
Remove explicit filename from Doxygen @file commands.
diego
parents:
11560
diff
changeset
|
23 * @file |
| 6738 | 24 * Dirac encoder support via libschroedinger-1.0 libraries. More details about |
| 25 * the Schroedinger project can be found at http://www.diracvideo.org/. | |
| 26 * The library implements Dirac Specification Version 2.2 | |
| 27 * (http://dirac.sourceforge.net/specification.html). | |
| 28 */ | |
| 29 | |
| 30 #undef NDEBUG | |
| 31 #include <assert.h> | |
| 32 | |
| 33 #include <schroedinger/schro.h> | |
| 34 #include <schroedinger/schrodebug.h> | |
| 35 #include <schroedinger/schrovideoformat.h> | |
| 36 | |
| 37 #include "avcodec.h" | |
| 38 #include "libdirac_libschro.h" | |
| 39 #include "libschroedinger.h" | |
| 40 | |
| 41 | |
| 42 /** libschroedinger encoder private data */ | |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
43 typedef struct FfmpegSchroEncoderParams { |
| 6738 | 44 /** Schroedinger video format */ |
| 45 SchroVideoFormat *format; | |
| 46 | |
| 47 /** Schroedinger frame format */ | |
| 48 SchroFrameFormat frame_format; | |
| 49 | |
| 50 /** frame being encoded */ | |
| 51 AVFrame picture; | |
| 52 | |
| 53 /** frame size */ | |
| 54 int frame_size; | |
| 55 | |
| 56 /** Schroedinger encoder handle*/ | |
| 57 SchroEncoder* encoder; | |
| 58 | |
|
7253
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
59 /** buffer to store encoder output before writing it to the frame queue*/ |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
60 unsigned char *enc_buf; |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
61 |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
62 /** Size of encoder buffer*/ |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
63 int enc_buf_size; |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
64 |
| 6738 | 65 /** queue storing encoded frames */ |
| 66 FfmpegDiracSchroQueue enc_frame_queue; | |
| 67 | |
| 68 /** end of sequence signalled */ | |
| 69 int eos_signalled; | |
| 70 | |
| 71 /** end of sequence pulled */ | |
| 72 int eos_pulled; | |
| 73 } FfmpegSchroEncoderParams; | |
| 74 | |
| 75 /** | |
| 76 * Works out Schro-compatible chroma format. | |
| 77 */ | |
| 78 static int SetSchroChromaFormat(AVCodecContext *avccontext) | |
| 79 { | |
| 80 int num_formats = sizeof(ffmpeg_schro_pixel_format_map) / | |
| 81 sizeof(ffmpeg_schro_pixel_format_map[0]); | |
| 82 int idx; | |
| 83 | |
| 84 FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data; | |
| 85 | |
| 86 for (idx = 0; idx < num_formats; ++idx) { | |
| 87 if (ffmpeg_schro_pixel_format_map[idx].ff_pix_fmt == | |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
88 avccontext->pix_fmt) { |
| 6738 | 89 p_schro_params->format->chroma_format = |
| 90 ffmpeg_schro_pixel_format_map[idx].schro_pix_fmt; | |
| 91 return 0; | |
| 92 } | |
| 93 } | |
| 94 | |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
95 av_log(avccontext, AV_LOG_ERROR, |
|
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
96 "This codec currently only supports planar YUV 4:2:0, 4:2:2" |
|
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
97 " and 4:4:4 formats.\n"); |
| 6738 | 98 |
| 99 return -1; | |
| 100 } | |
| 101 | |
| 102 static int libschroedinger_encode_init(AVCodecContext *avccontext) | |
| 103 { | |
| 104 FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data; | |
| 105 SchroVideoFormatEnum preset; | |
| 106 | |
| 107 /* Initialize the libraries that libschroedinger depends on. */ | |
| 108 schro_init(); | |
| 109 | |
| 110 /* Create an encoder object. */ | |
| 111 p_schro_params->encoder = schro_encoder_new(); | |
| 112 | |
| 113 if (!p_schro_params->encoder) { | |
| 114 av_log(avccontext, AV_LOG_ERROR, | |
| 115 "Unrecoverable Error: schro_encoder_new failed. "); | |
| 116 return -1; | |
| 117 } | |
| 118 | |
| 119 /* Initialize the format. */ | |
| 120 preset = ff_get_schro_video_format_preset(avccontext); | |
| 121 p_schro_params->format = | |
| 122 schro_encoder_get_video_format(p_schro_params->encoder); | |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
123 schro_video_format_set_std_video_format(p_schro_params->format, preset); |
|
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
124 p_schro_params->format->width = avccontext->width; |
| 6738 | 125 p_schro_params->format->height = avccontext->height; |
| 126 | |
| 127 if (SetSchroChromaFormat(avccontext) == -1) | |
| 128 return -1; | |
| 129 | |
| 130 if (ff_get_schro_frame_format(p_schro_params->format->chroma_format, | |
| 131 &p_schro_params->frame_format) == -1) { | |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
132 av_log(avccontext, AV_LOG_ERROR, |
|
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
133 "This codec currently supports only planar YUV 4:2:0, 4:2:2" |
|
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
134 " and 4:4:4 formats.\n"); |
| 6738 | 135 return -1; |
| 136 } | |
| 137 | |
| 138 p_schro_params->format->frame_rate_numerator = avccontext->time_base.den; | |
| 139 p_schro_params->format->frame_rate_denominator = avccontext->time_base.num; | |
| 140 | |
| 141 p_schro_params->frame_size = avpicture_get_size(avccontext->pix_fmt, | |
| 142 avccontext->width, | |
| 143 avccontext->height); | |
| 144 | |
| 145 avccontext->coded_frame = &p_schro_params->picture; | |
| 146 | |
| 10055 | 147 if (!avccontext->gop_size) { |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
148 schro_encoder_setting_set_double(p_schro_params->encoder, |
|
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
149 "gop_structure", |
|
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
150 SCHRO_ENCODER_GOP_INTRA_ONLY); |
|
7839
e6348a5656e0
Add support for creating Simple Profile (I-frame only, no arithmetic coding)
diego
parents:
7253
diff
changeset
|
151 |
|
10056
646065f63290
Remove useless braces around if/for/while expressions.
diego
parents:
10055
diff
changeset
|
152 if (avccontext->coder_type == FF_CODER_TYPE_VLC) |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
153 schro_encoder_setting_set_double(p_schro_params->encoder, |
|
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
154 "enable_noarith", 1); |
|
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
155 } else { |
| 11682 | 156 schro_encoder_setting_set_double(p_schro_params->encoder, |
| 157 "au_distance", avccontext->gop_size); | |
| 6738 | 158 avccontext->has_b_frames = 1; |
| 159 } | |
| 160 | |
| 161 /* FIXME - Need to handle SCHRO_ENCODER_RATE_CONTROL_LOW_DELAY. */ | |
| 162 if (avccontext->flags & CODEC_FLAG_QSCALE) { | |
| 10055 | 163 if (!avccontext->global_quality) { |
| 6738 | 164 /* lossless coding */ |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
165 schro_encoder_setting_set_double(p_schro_params->encoder, |
|
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
166 "rate_control", |
|
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
167 SCHRO_ENCODER_RATE_CONTROL_LOSSLESS); |
| 6738 | 168 } else { |
|
11681
04c086666e50
schroenc: Use constant quality for constant quality, not noise threshold
conrad
parents:
11680
diff
changeset
|
169 int quality; |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
170 schro_encoder_setting_set_double(p_schro_params->encoder, |
|
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
171 "rate_control", |
|
11681
04c086666e50
schroenc: Use constant quality for constant quality, not noise threshold
conrad
parents:
11680
diff
changeset
|
172 SCHRO_ENCODER_RATE_CONTROL_CONSTANT_QUALITY); |
| 6738 | 173 |
|
11681
04c086666e50
schroenc: Use constant quality for constant quality, not noise threshold
conrad
parents:
11680
diff
changeset
|
174 quality = avccontext->global_quality / FF_QP2LAMBDA; |
|
04c086666e50
schroenc: Use constant quality for constant quality, not noise threshold
conrad
parents:
11680
diff
changeset
|
175 if (quality > 10) |
|
04c086666e50
schroenc: Use constant quality for constant quality, not noise threshold
conrad
parents:
11680
diff
changeset
|
176 quality = 10; |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
177 schro_encoder_setting_set_double(p_schro_params->encoder, |
|
11681
04c086666e50
schroenc: Use constant quality for constant quality, not noise threshold
conrad
parents:
11680
diff
changeset
|
178 "quality", quality); |
| 6738 | 179 } |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
180 } else { |
|
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
181 schro_encoder_setting_set_double(p_schro_params->encoder, |
|
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
182 "rate_control", |
|
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
183 SCHRO_ENCODER_RATE_CONTROL_CONSTANT_BITRATE); |
| 6738 | 184 |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
185 schro_encoder_setting_set_double(p_schro_params->encoder, |
|
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
186 "bitrate", |
|
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
187 avccontext->bit_rate); |
| 6738 | 188 |
| 189 } | |
| 190 | |
|
10056
646065f63290
Remove useless braces around if/for/while expressions.
diego
parents:
10055
diff
changeset
|
191 if (avccontext->flags & CODEC_FLAG_INTERLACED_ME) |
| 6738 | 192 /* All material can be coded as interlaced or progressive |
| 193 irrespective of the type of source material. */ | |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
194 schro_encoder_setting_set_double(p_schro_params->encoder, |
|
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
195 "interlaced_coding", 1); |
| 6738 | 196 |
| 11683 | 197 schro_encoder_setting_set_double(p_schro_params->encoder, "open_gop", |
| 198 !(avccontext->flags & CODEC_FLAG_CLOSED_GOP)); | |
| 199 | |
| 6738 | 200 /* FIXME: Signal range hardcoded to 8-bit data until both libschroedinger |
| 201 * and libdirac support other bit-depth data. */ | |
| 202 schro_video_format_set_std_signal_range(p_schro_params->format, | |
| 203 SCHRO_SIGNAL_RANGE_8BIT_VIDEO); | |
| 204 | |
| 205 /* Set the encoder format. */ | |
| 206 schro_encoder_set_video_format(p_schro_params->encoder, | |
| 207 p_schro_params->format); | |
| 208 | |
| 209 /* Set the debug level. */ | |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
210 schro_debug_set_level(avccontext->debug); |
| 6738 | 211 |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
212 schro_encoder_start(p_schro_params->encoder); |
| 6738 | 213 |
| 214 /* Initialize the encoded frame queue. */ | |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
215 ff_dirac_schro_queue_init(&p_schro_params->enc_frame_queue); |
|
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
216 return 0; |
| 6738 | 217 } |
| 218 | |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
219 static SchroFrame *libschroedinger_frame_from_data(AVCodecContext *avccontext, |
|
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
220 void *in_data) |
| 6738 | 221 { |
| 222 FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data; | |
| 223 SchroFrame *in_frame; | |
| 224 /* Input line size may differ from what the codec supports. Especially | |
| 225 * when transcoding from one format to another. So use avpicture_layout | |
| 226 * to copy the frame. */ | |
|
10061
09f2db2d7c90
Fix bug caused by difference in stride and picture width.
diego
parents:
10060
diff
changeset
|
227 in_frame = ff_create_schro_frame(avccontext, p_schro_params->frame_format); |
| 6738 | 228 |
|
10061
09f2db2d7c90
Fix bug caused by difference in stride and picture width.
diego
parents:
10060
diff
changeset
|
229 if (in_frame) |
|
09f2db2d7c90
Fix bug caused by difference in stride and picture width.
diego
parents:
10060
diff
changeset
|
230 avpicture_layout((AVPicture *)in_data, avccontext->pix_fmt, |
|
09f2db2d7c90
Fix bug caused by difference in stride and picture width.
diego
parents:
10060
diff
changeset
|
231 avccontext->width, avccontext->height, |
|
09f2db2d7c90
Fix bug caused by difference in stride and picture width.
diego
parents:
10060
diff
changeset
|
232 in_frame->components[0].data, |
|
09f2db2d7c90
Fix bug caused by difference in stride and picture width.
diego
parents:
10060
diff
changeset
|
233 p_schro_params->frame_size); |
| 6738 | 234 |
| 235 return in_frame; | |
| 236 } | |
| 237 | |
| 238 static void SchroedingerFreeFrame(void *data) | |
| 239 { | |
| 240 FfmpegDiracSchroEncodedFrame *enc_frame = data; | |
| 241 | |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
242 av_freep(&(enc_frame->p_encbuf)); |
| 6738 | 243 av_free(enc_frame); |
| 244 } | |
| 245 | |
| 246 static int libschroedinger_encode_frame(AVCodecContext *avccontext, | |
| 247 unsigned char *frame, | |
| 248 int buf_size, void *data) | |
| 249 { | |
| 250 int enc_size = 0; | |
| 251 FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data; | |
| 252 SchroEncoder *encoder = p_schro_params->encoder; | |
| 253 struct FfmpegDiracSchroEncodedFrame* p_frame_output = NULL; | |
| 254 int go = 1; | |
| 255 SchroBuffer *enc_buf; | |
| 256 int presentation_frame; | |
| 257 int parse_code; | |
|
7253
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
258 int last_frame_in_sequence = 0; |
| 6738 | 259 |
|
10058
d18468c5fee0
Simplify another 'if' condition: Replace 'exp == 0' by '!exp'.
diego
parents:
10056
diff
changeset
|
260 if (!data) { |
| 6738 | 261 /* Push end of sequence if not already signalled. */ |
| 262 if (!p_schro_params->eos_signalled) { | |
| 263 schro_encoder_end_of_stream(encoder); | |
| 264 p_schro_params->eos_signalled = 1; | |
| 265 } | |
| 266 } else { | |
| 267 /* Allocate frame data to schro input buffer. */ | |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
268 SchroFrame *in_frame = libschroedinger_frame_from_data(avccontext, |
|
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
269 data); |
| 6738 | 270 /* Load next frame. */ |
| 271 schro_encoder_push_frame(encoder, in_frame); | |
| 272 } | |
| 273 | |
| 274 if (p_schro_params->eos_pulled) | |
| 275 go = 0; | |
| 276 | |
| 277 /* Now check to see if we have any output from the encoder. */ | |
| 278 while (go) { | |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
279 SchroStateEnum state; |
| 6738 | 280 state = schro_encoder_wait(encoder); |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
281 switch (state) { |
| 6738 | 282 case SCHRO_STATE_HAVE_BUFFER: |
| 283 case SCHRO_STATE_END_OF_STREAM: | |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
284 enc_buf = schro_encoder_pull(encoder, &presentation_frame); |
|
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
285 assert(enc_buf->length > 0); |
|
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
286 assert(enc_buf->length <= buf_size); |
|
7253
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
287 parse_code = enc_buf->data[4]; |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
288 |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
289 /* All non-frame data is prepended to actual frame data to |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
290 * be able to set the pts correctly. So we don't write data |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
291 * to the frame output queue until we actually have a frame |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
292 */ |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
293 p_schro_params->enc_buf = av_realloc(p_schro_params->enc_buf, |
|
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
294 p_schro_params->enc_buf_size + enc_buf->length); |
|
7253
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
295 |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
296 memcpy(p_schro_params->enc_buf + p_schro_params->enc_buf_size, |
|
7253
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
297 enc_buf->data, enc_buf->length); |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
298 p_schro_params->enc_buf_size += enc_buf->length; |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
299 |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
300 |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
301 if (state == SCHRO_STATE_END_OF_STREAM) { |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
302 p_schro_params->eos_pulled = 1; |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
303 go = 0; |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
304 } |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
305 |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
306 if (!SCHRO_PARSE_CODE_IS_PICTURE(parse_code)) { |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
307 schro_buffer_unref(enc_buf); |
|
7253
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
308 break; |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
309 } |
| 6738 | 310 |
| 311 /* Create output frame. */ | |
| 312 p_frame_output = av_mallocz(sizeof(FfmpegDiracSchroEncodedFrame)); | |
| 313 /* Set output data. */ | |
|
7253
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
314 p_frame_output->size = p_schro_params->enc_buf_size; |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
315 p_frame_output->p_encbuf = p_schro_params->enc_buf; |
| 6738 | 316 if (SCHRO_PARSE_CODE_IS_INTRA(parse_code) && |
|
10056
646065f63290
Remove useless braces around if/for/while expressions.
diego
parents:
10055
diff
changeset
|
317 SCHRO_PARSE_CODE_IS_REFERENCE(parse_code)) |
| 6738 | 318 p_frame_output->key_frame = 1; |
| 319 | |
| 320 /* Parse the coded frame number from the bitstream. Bytes 14 | |
| 321 * through 17 represesent the frame number. */ | |
| 322 p_frame_output->frame_num = (enc_buf->data[13] << 24) + | |
| 323 (enc_buf->data[14] << 16) + | |
| 324 (enc_buf->data[15] << 8) + | |
| 325 enc_buf->data[16]; | |
| 326 | |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
327 ff_dirac_schro_queue_push_back(&p_schro_params->enc_frame_queue, |
|
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
328 p_frame_output); |
|
7253
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
329 p_schro_params->enc_buf_size = 0; |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
330 p_schro_params->enc_buf = NULL; |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
331 |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
332 schro_buffer_unref(enc_buf); |
| 6738 | 333 |
| 334 break; | |
| 335 | |
| 336 case SCHRO_STATE_NEED_FRAME: | |
| 337 go = 0; | |
| 338 break; | |
| 339 | |
| 340 case SCHRO_STATE_AGAIN: | |
| 341 break; | |
| 342 | |
| 343 default: | |
| 344 av_log(avccontext, AV_LOG_ERROR, "Unknown Schro Encoder state\n"); | |
| 345 return -1; | |
| 346 } | |
| 347 } | |
| 348 | |
| 349 /* Copy 'next' frame in queue. */ | |
|
7253
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
350 |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
351 if (p_schro_params->enc_frame_queue.size == 1 && |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
352 p_schro_params->eos_pulled) |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
353 last_frame_in_sequence = 1; |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
354 |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
355 p_frame_output = ff_dirac_schro_queue_pop(&p_schro_params->enc_frame_queue); |
| 6738 | 356 |
| 10055 | 357 if (!p_frame_output) |
| 6738 | 358 return 0; |
| 359 | |
| 360 memcpy(frame, p_frame_output->p_encbuf, p_frame_output->size); | |
| 361 avccontext->coded_frame->key_frame = p_frame_output->key_frame; | |
| 362 /* Use the frame number of the encoded frame as the pts. It is OK to | |
| 363 * do so since Dirac is a constant frame rate codec. It expects input | |
| 364 * to be of constant frame rate. */ | |
| 365 avccontext->coded_frame->pts = p_frame_output->frame_num; | |
| 366 enc_size = p_frame_output->size; | |
| 367 | |
|
7253
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
368 /* Append the end of sequence information to the last frame in the |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
369 * sequence. */ |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
370 if (last_frame_in_sequence && p_schro_params->enc_buf_size > 0) { |
|
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
371 memcpy(frame + enc_size, p_schro_params->enc_buf, |
|
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
372 p_schro_params->enc_buf_size); |
|
7253
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
373 enc_size += p_schro_params->enc_buf_size; |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
374 av_freep(&p_schro_params->enc_buf); |
|
7253
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
375 p_schro_params->enc_buf_size = 0; |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
376 } |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
377 |
| 6738 | 378 /* free frame */ |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
379 SchroedingerFreeFrame(p_frame_output); |
| 6738 | 380 |
| 381 return enc_size; | |
| 382 } | |
| 383 | |
| 384 | |
| 385 static int libschroedinger_encode_close(AVCodecContext *avccontext) | |
| 386 { | |
| 387 | |
| 388 FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data; | |
| 389 | |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
390 /* Close the encoder. */ |
| 6738 | 391 schro_encoder_free(p_schro_params->encoder); |
| 392 | |
| 393 /* Free data in the output frame queue. */ | |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
394 ff_dirac_schro_queue_free(&p_schro_params->enc_frame_queue, |
|
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
395 SchroedingerFreeFrame); |
| 6738 | 396 |
|
7253
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
397 |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
398 /* Free the encoder buffer. */ |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
399 if (p_schro_params->enc_buf_size) |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
400 av_freep(&p_schro_params->enc_buf); |
|
2f5b98d0aa13
Fix pts handling when encoding with libschroedinger, closes issue 453.
diego
parents:
7040
diff
changeset
|
401 |
| 6738 | 402 /* Free the video format structure. */ |
| 403 av_freep(&p_schro_params->format); | |
| 404 | |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
405 return 0; |
| 6738 | 406 } |
| 407 | |
| 408 | |
| 409 AVCodec libschroedinger_encoder = { | |
| 410 "libschroedinger", | |
|
11560
8a4984c5cacc
Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents:
10146
diff
changeset
|
411 AVMEDIA_TYPE_VIDEO, |
| 6738 | 412 CODEC_ID_DIRAC, |
| 413 sizeof(FfmpegSchroEncoderParams), | |
| 414 libschroedinger_encode_init, | |
| 415 libschroedinger_encode_frame, | |
| 416 libschroedinger_encode_close, | |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
417 .capabilities = CODEC_CAP_DELAY, |
|
10146
38cfe222e1a4
Mark all pix_fmts and supported_framerates compound literals as const.
reimar
parents:
10061
diff
changeset
|
418 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV444P, PIX_FMT_NONE}, |
|
10060
965220ebc611
cosmetics: indentation, prettyprinting, K&R coding style
diego
parents:
10058
diff
changeset
|
419 .long_name = NULL_IF_CONFIG_SMALL("libschroedinger Dirac 2.2"), |
| 6738 | 420 }; |
