History

avcodec/mjpegenc: Include all supported pix_fmts in mpegenc pix_fmts

Currently said list contains only the pixel formats that are always supported irrespective of the range and the value of strict_std_compliance. This makes the MJPEG encoder an outlier as all other codecs put all potentially supported pixel formats into said list and error out if the chosen pixel format is unsupported. This commit brings it therefore in line with the other encoders. The behaviour of fftools/ffmpeg_filter.c has been preserved. A more informed decision would be possible if colour range were available at this point, but it isn't.

Andreas Rheinhardt committed 5 years ago

Commit 059fc2d1 parent 48cda7d7 files · +40 −55Browse files

Unified · Split

7 files
  1. fftools/ffmpeg_filter.c +7 −13
  2. libavcodec/encode.c +1 −3
  3. libavcodec/ljpegenc.c +3 −11
  4. libavcodec/mjpegenc.c +10 −1
  5. libavcodec/mjpegenc_common.c +16 −0
  6. libavcodec/mjpegenc_common.h +2 −0
  7. libavcodec/mpegvideo_enc.c +1 −27
fftools/ffmpeg_filter.c+7−13
@@ -39,22 +39,16 @@ · 38 unmodified lines
39#include "libavutil/imgutils.h"39#include "libavutil/imgutils.h"
40#include "libavutil/samplefmt.h"40#include "libavutil/samplefmt.h"
41 41
42static const enum AVPixelFormat *get_compliance_unofficial_pix_fmts(enum AVCodecID codec_id, const enum AVPixelFormat default_formats[])42// FIXME: YUV420P etc. are actually supported with full color range,
43// yet the latter information isn't available here.
44static const enum AVPixelFormat *get_compliance_normal_pix_fmts(enum AVCodecID codec_id, const enum AVPixelFormat default_formats[])
43{45{
44 static const enum AVPixelFormat mjpeg_formats[] =46 static const enum AVPixelFormat mjpeg_formats[] =
45 { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,47 { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
46 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
47 AV_PIX_FMT_NONE };48 AV_PIX_FMT_NONE };
48 static const enum AVPixelFormat ljpeg_formats[] =
49 { AV_PIX_FMT_BGR24 , AV_PIX_FMT_BGRA , AV_PIX_FMT_BGR0,
50 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P,
51 AV_PIX_FMT_YUV420P , AV_PIX_FMT_YUV444P , AV_PIX_FMT_YUV422P,
52 AV_PIX_FMT_NONE};
53 49
54 if (codec_id == AV_CODEC_ID_MJPEG) {50 if (codec_id == AV_CODEC_ID_MJPEG) {
55 return mjpeg_formats;51 return mjpeg_formats;
56 } else if (codec_id == AV_CODEC_ID_LJPEG) {
57 return ljpeg_formats;
58 } else {52 } else {
59 return default_formats;53 return default_formats;
60 }54 }
@@ -70,8 +64,8 @@ static enum AVPixelFormat choose_pixel_fmt(AVStream *st, AVCodecContext *enc_ctx · 9 unmodified lines
70 int has_alpha = desc ? desc->nb_components % 2 == 0 : 0;64 int has_alpha = desc ? desc->nb_components % 2 == 0 : 0;
71 enum AVPixelFormat best= AV_PIX_FMT_NONE;65 enum AVPixelFormat best= AV_PIX_FMT_NONE;
72 66
73 if (enc_ctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {67 if (enc_ctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
74 p = get_compliance_unofficial_pix_fmts(enc_ctx->codec_id, p);68 p = get_compliance_normal_pix_fmts(enc_ctx->codec_id, p);
75 }69 }
76 for (; *p != AV_PIX_FMT_NONE; p++) {70 for (; *p != AV_PIX_FMT_NONE; p++) {
77 best = av_find_best_pix_fmt_of_2(best, *p, target, has_alpha, NULL);71 best = av_find_best_pix_fmt_of_2(best, *p, target, has_alpha, NULL);
@@ -118,8 +112,8 @@ static char *choose_pix_fmts(OutputFilter *ofilter) · 40 unmodified lines
118 exit_program(1);112 exit_program(1);
119 113
120 p = ost->enc->pix_fmts;114 p = ost->enc->pix_fmts;
121 if (ost->enc_ctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {115 if (ost->enc_ctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
122 p = get_compliance_unofficial_pix_fmts(ost->enc_ctx->codec_id, p);116 p = get_compliance_normal_pix_fmts(ost->enc_ctx->codec_id, p);
123 }117 }
124 118
125 for (; *p != AV_PIX_FMT_NONE; p++) {119 for (; *p != AV_PIX_FMT_NONE; p++) {
libavcodec/encode.c+1−3
@@ -564,9 +564,7 @@ FF_ENABLE_DEPRECATION_WARNINGS · 563 unmodified lines
564 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)564 for (i = 0; avctx->codec->pix_fmts[i] != AV_PIX_FMT_NONE; i++)
565 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])565 if (avctx->pix_fmt == avctx->codec->pix_fmts[i])
566 break;566 break;
567 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE567 if (avctx->codec->pix_fmts[i] == AV_PIX_FMT_NONE) {
568 && !(avctx->codec_id == AV_CODEC_ID_MJPEG
569 && avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL)) {
570 char buf[128];568 char buf[128];
571 snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);569 snprintf(buf, sizeof(buf), "%d", avctx->pix_fmt);
572 av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",570 av_log(avctx, AV_LOG_ERROR, "Specified pixel format %s is invalid or not supported\n",
libavcodec/ljpegenc.c+3−11
@@ -289,19 +289,11 @@ static av_cold int ljpeg_encode_close(AVCodecContext *avctx) · 288 unmodified lines
289 289
290static av_cold int ljpeg_encode_init(AVCodecContext *avctx)290static av_cold int ljpeg_encode_init(AVCodecContext *avctx)
291{291{
292 int ret = ff_mjpeg_encode_check_pix_fmt(avctx);
292 LJpegEncContext *s = avctx->priv_data;293 LJpegEncContext *s = avctx->priv_data;
293 294
294 if ((avctx->pix_fmt == AV_PIX_FMT_YUV420P ||295 if (ret < 0)
295 avctx->pix_fmt == AV_PIX_FMT_YUV422P ||296 return ret;
296 avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
297 avctx->color_range == AVCOL_RANGE_MPEG) &&
298 avctx->color_range != AVCOL_RANGE_JPEG &&
299 avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
300 av_log(avctx, AV_LOG_ERROR,
301 "Non full-range YUV is non-standard, set strict_std_compliance "
302 "to at most unofficial to use it.\n");
303 return AVERROR(EINVAL);
304 }
305 297
306#if FF_API_CODED_FRAME298#if FF_API_CODED_FRAME
307FF_DISABLE_DEPRECATION_WARNINGS299FF_DISABLE_DEPRECATION_WARNINGS
libavcodec/mjpegenc.c+10−1
@@ -261,9 +261,16 @@ static int alloc_huffman(MpegEncContext *s) · 260 unmodified lines
261av_cold int ff_mjpeg_encode_init(MpegEncContext *s)261av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
262{262{
263 MJpegContext *m;263 MJpegContext *m;
264 int ret;
264 265
265 av_assert0(s->slice_context_count == 1);266 av_assert0(s->slice_context_count == 1);
266 267
268 /* The following check is automatically true for AMV,
269 * but it doesn't hurt either. */
270 ret = ff_mjpeg_encode_check_pix_fmt(s->avctx);
271 if (ret < 0)
272 return ret;
273
267 if (s->width > 65500 || s->height > 65500) {274 if (s->width > 65500 || s->height > 65500) {
268 av_log(s, AV_LOG_ERROR, "JPEG does not support resolutions above 65500x65500\n");275 av_log(s, AV_LOG_ERROR, "JPEG does not support resolutions above 65500x65500\n");
269 return AVERROR(EINVAL);276 return AVERROR(EINVAL);
@@ -609,7 +616,9 @@ AVCodec ff_mjpeg_encoder = { · 339 unmodified lines
609 .capabilities = AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS,616 .capabilities = AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS,
610 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,617 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
611 .pix_fmts = (const enum AVPixelFormat[]) {618 .pix_fmts = (const enum AVPixelFormat[]) {
612 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_NONE619 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P,
620 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P,
621 AV_PIX_FMT_NONE
613 },622 },
614 .priv_class = &mjpeg_class,623 .priv_class = &mjpeg_class,
615 .profiles = NULL_IF_CONFIG_SMALL(ff_mjpeg_profiles),624 .profiles = NULL_IF_CONFIG_SMALL(ff_mjpeg_profiles),
libavcodec/mjpegenc_common.c+16−0
@@ -436,3 +436,19 @@ void ff_mjpeg_encode_dc(PutBitContext *pb, int val, · 435 unmodified lines
436 put_sbits(pb, nbits, mant);436 put_sbits(pb, nbits, mant);
437 }437 }
438}438}
439
440int ff_mjpeg_encode_check_pix_fmt(AVCodecContext *avctx)
441{
442 if (avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL &&
443 avctx->color_range != AVCOL_RANGE_JPEG &&
444 (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
445 avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
446 avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
447 avctx->color_range == AVCOL_RANGE_MPEG)) {
448 av_log(avctx, AV_LOG_ERROR,
449 "Non full-range YUV is non-standard, set strict_std_compliance "
450 "to at most unofficial to use it.\n");
451 return AVERROR(EINVAL);
452 }
453 return 0;
454}
libavcodec/mjpegenc_common.h+2−0
@@ -41,4 +41,6 @@ void ff_mjpeg_init_hvsample(AVCodecContext *avctx, int hsample[4], int vsample[4 · 40 unmodified lines
41void ff_mjpeg_encode_dc(PutBitContext *pb, int val,41void ff_mjpeg_encode_dc(PutBitContext *pb, int val,
42 uint8_t *huff_size, uint16_t *huff_code);42 uint8_t *huff_size, uint16_t *huff_code);
43 43
44int ff_mjpeg_encode_check_pix_fmt(AVCodecContext *avctx);
45
44#endif /* AVCODEC_MJPEGENC_COMMON_H */46#endif /* AVCODEC_MJPEGENC_COMMON_H */
libavcodec/mpegvideo_enc.c+1−27
@@ -297,36 +297,10 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) · 296 unmodified lines
297{297{
298 MpegEncContext *s = avctx->priv_data;298 MpegEncContext *s = avctx->priv_data;
299 AVCPBProperties *cpb_props;299 AVCPBProperties *cpb_props;
300 int i, ret, format_supported;300 int i, ret;
301 301
302 mpv_encode_defaults(s);302 mpv_encode_defaults(s);
303 303
304 switch (avctx->codec_id) {
305 case AV_CODEC_ID_MJPEG:
306 format_supported = 0;
307 /* JPEG color space */
308 if (avctx->pix_fmt == AV_PIX_FMT_YUVJ420P ||
309 avctx->pix_fmt == AV_PIX_FMT_YUVJ422P ||
310 avctx->pix_fmt == AV_PIX_FMT_YUVJ444P ||
311 (avctx->color_range == AVCOL_RANGE_JPEG &&
312 (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
313 avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
314 avctx->pix_fmt == AV_PIX_FMT_YUV444P)))
315 format_supported = 1;
316 /* MPEG color space */
317 else if (avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL &&
318 (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
319 avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
320 avctx->pix_fmt == AV_PIX_FMT_YUV444P))
321 format_supported = 1;
322
323 if (!format_supported) {
324 av_log(avctx, AV_LOG_ERROR, "colorspace not supported in jpeg\n");
325 return AVERROR(EINVAL);
326 }
327 break;
328 }
329
330 switch (avctx->pix_fmt) {304 switch (avctx->pix_fmt) {
331 case AV_PIX_FMT_YUVJ444P:305 case AV_PIX_FMT_YUVJ444P:
332 case AV_PIX_FMT_YUV444P:306 case AV_PIX_FMT_YUV444P:

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t